我对派生自 的类有疑问Service
,它是 ServiceStack 库的一部分。如果我设置了一个派生自or方法Service
并将其放置在其中的单独类,那么一切都运行良好,但是,问题是该类本身没有对任何业务逻辑的引用。只要我返回静态数据就可以了,但是如果我想将它集成到业务逻辑中,那就行不通了。我希望 Get 方法属于我的业务逻辑所在的同一类。那是糟糕的设计吗?如果我能做些什么来解决它?我得到的错误是派生自的类无论出于何种原因都被实例化(根据我目前的理解,这对我来说意义不大)。应该类,源自Get
Any
Service
Service
,不就是理清路由逻辑吗?
这里有一些代码来说明我的问题:
这段代码运行良好,但问题是类DTO
对类的内容一无所知ClassWithBusinessLogic
:
public class ClassWithBusinessLogic
{
public ClassWithBusinessLogic()
{
string hostAddress = "http://localhost:1337/";
WebServiceHost host = new WebServiceHost("MattHost", new List<Assembly>() { typeof(DTOs).Assembly });
host.StartWebService(hostAddress);
Console.WriteLine("Host started listening....");
Console.ReadKey();
}
}
public class HelloWorldRequest : IReturn<string>
{
public string FirstWord { get; set; }
public HelloWorldRequest(string firstWord)
{
FirstWord = firstWord;
}
}
public class DTO : Service
{
public string Get(HelloWorldRequest request)
{
return request.FirstWord + " World!!!";
}
}
现在,以下实际上是我想要的,但是代码的行为出乎意料,基本上它不起作用:
public class ClassWithBusinessLogic : Service
{
private string SomeBusinessLogic { get; set; }
public ClassWithBusinessLogic()
{
string hostAddress = "http://localhost:1337/";
//Simplistic business logic
SomeBusinessLogic = "Hello";
WebServiceHost host = new WebServiceHost("MyHost", new List<Assembly>() { typeof(DTO).Assembly });
host.StartWebService(hostAddress);
Console.WriteLine("Host started listening....");
Console.ReadKey();
}
public string Get(HelloWorldRequest request)
{
return SomeBusinessLogic;
}
}
public class HelloWorldRequest : IReturn<string>
{
public string FirstWord { get; set; }
public HelloWorldRequest(string firstWord)
{
FirstWord = firstWord;
}
}
为了运行以下类也需要:
public class WebServiceHost : AppHostHttpListenerBase
{
public WebServiceHost(string hostName, List<Assembly> assembliesWithServices) : base(hostName, assembliesWithServices.ToArray())
{
base.Init();
}
public override void Configure(Funq.Container container)
{ }
public void StartWebService(string hostAddress)
{
base.Start(hostAddress);
}
public void StopWebService()
{
base.Stop();
}
}
public class WebServiceClient
{
private JsonServiceClient Client { get; set; }
public WebServiceClient(string hostAddress)
{
Client = new JsonServiceClient(hostAddress);
}
public ResponseType Get<ResponseType>(dynamic request)
{
return Client.Get<ResponseType>(request);
}
public void GetAsync<ResponseType>(dynamic request, Action<ResponseType> callback, Action<ResponseType, Exception> onError)
{
Client.GetAsync<ResponseType>(request, callback, onError);
}
}
class Client_Entry
{
static void Main(string[] args)
{
Client client = new Client();
}
}
public class Client
{
public Client()
{
Console.WriteLine("This is the web client. Press key to start requests");
Console.ReadKey();
string hostAddress = "http://localhost:1337/";
WebServiceClient client = new WebServiceClient(hostAddress);
var result = client.Get<string>(new HelloWorldRequest("Hello"));
Console.WriteLine("Result: " + result);
Console.WriteLine("Finished all requests. Press key to quit");
Console.ReadKey();
}
private void OnResponse(HelloWorldRequest response)
{
Console.WriteLine(response.FirstWord);
}
private void OnError(HelloWorldRequest response, Exception exception)
{
Console.WriteLine("Error. Exception Message : " + exception.Message);
}
}