ServiceStack,在哪里放置业务逻辑?

Mat*_*olf 5 c# json host http servicestack

我遇到的派生类有问题Service,这是ServiceStack库的一部分.如果我设置一个单独的类派生Service并放置Get或其中的Any方法然后一切运行正常,但问题是该类本身没有引用任何业务逻辑.只要我返回静态数据就可以了,但是如果我想将它集成到业务逻辑中那么那就行不通了.我希望Get方法成为我的业务逻辑所在的同一个类的一部分.这是不好的设计,如果我可以做些什么来解决它?我得到的错误是派生的类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!!!";
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,以下实际上是我想要的,但代码行为意外,基本上它不起作用:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

为了运行,还需要以下类:

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

}
Run Code Online (Sandbox Code Playgroud)

Mik*_*ugh 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();
}
Run Code Online (Sandbox Code Playgroud)

然后,该构造函数实例化 ServiceStack WebServiceHost,并传入其自己的程序集 - 这使我相信 ServiceStack 随后将重新初始化 ClassWithBusinessLogic(),因为它继承自 Service,可能会陷入无限循环。

您需要区分您的关注点 - 您的 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.
Run Code Online (Sandbox Code Playgroud)