我遇到了來自Service
的類的問題,它是ServiceStack庫的一部分。如果我設置了一個從Service
派生出來的單獨的類,並且將Get
或Any
方法放入其中,那麼所有內容都可以正常運行,但是,問題在於該類本身沒有引用任何業務邏輯。只要我返回靜態數據就可以,但如果我想將其集成到業務邏輯中,那麼這是行不通的。我希望Get方法成爲業務邏輯所在的同一類的一部分。這是不好的設計,如果我能做些什麼來解決它?我得到的錯誤是,從Service
派生的類無論出於何種原因(根據我目前的理解對我來說都沒什麼意義)得到實例化。該類應該來自Service
,而不僅僅是整理路由邏輯?ServiceStack,在哪裏放置業務邏輯?
這裏是一些代碼來說明我的問題:
此代碼運行正常,但問題是,類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;
}
}
爲了運行以下cl屁股也需要:
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);
}
}
你應該單獨代碼多一點,從服務派生還是應該只是網絡服務的問題,即解決一類 - 設置錯誤代碼,而不是業務邏輯,IMO。 – Mithir
公平點,但它並沒有回答我如何在ClassWithBusinessLogic中返回例如字符串'SomeBusinessLogic'的問題。 –