Service
ServiceStack ライブラリの一部であるから派生したクラスに問題があります。またはメソッドから派生し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);
}
}