2013-05-28 187 views
5

我遇到了來自Service的類的問題,它是ServiceStack庫的一部分。如果我設置了一個從Service派生出來的單獨的類,並且將GetAny方法放入其中,那麼所有內容都可以正常運行,但是,問題在於該類本身沒有引用任何業務邏輯。只要我返回靜態數據就可以,但如果我想將其集成到業務邏輯中,那麼這是行不通的。我希望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); 
    } 

} 
+0

你應該單獨代碼多一點,從服務派生還是應該只是網絡服務的問題,即解決一類 - 設置錯誤代碼,而不是業務邏輯,IMO。 – Mithir

+0

公平點,但它並沒有回答我如何在ClassWithBusinessLogic中返回例如字符串'SomeBusinessLogic'的問題。 –

回答

3

您的第二個代碼塊讀取的方式顯示您的服務主機和您的服務是同一個對象。當我看到

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(); 
} 

該構造函數,然後實例化ServiceStack WebServiceHost,通過在自己組裝 - 這使我相信,ServiceStack隨後將重新初始化ClassWithBusinessLogic(),因爲它從服務繼承,有可能下降無限循環。

您需要分離出您的問題 - 您的Web服務主機,Web服務和業務邏輯類都是分開的。將他們混合在一起,只會讓你感到沮喪。把他們分成他們自己的班級。您的業​​務邏輯可以通過IoC容器傳遞到您的Web服務中。

所以你最終的東西,如:

class BusinessLogicEngine : IBusinessLogic 
---> Define your business logic interface and implementation, and all required business object entities 

class WebService : Service 
    constructor(IBusinessLogic) 
---> Purely handles mapping incoming requests and outgoing responses to/from your business object entities. Recommend using AutoMapper (http://automapper.org/) 
---> Should also handle returning error codes to the client so as not to expose sensitive info (strack traces, IPs, etc) 


class WebServiceHost 
---> Constructor initializes the WebServiceHost with the typeof(WebService) from above. 
相關問題