ServiceStack CRUD Service routing Documentation

扶醉桌前 提交于 2021-02-19 07:08:25

问题


I am unable to find documentation (Clear or otherwise) that explains how to implement a Service in Service Stack with multiple methods. All examples show a service class with one method only. I cant imagine if you have a service with 4+ methods, that your really need to make 4+ service classes, 4+ Request DTO's and 4+ response DTO's. ( As shown here )

Example:

A Service with 4 Search methods ( method(Identifer1), method2(identifer2), method3(identifer3))

Based on the documented structure of a Request DTO ( Service Stack Wiki , and Creating your First Web Service ) , The Request DTO's Class Attributes, Control the Routing to the Service Class, (If I understood that correctly, but again I am looking for Documentation or an example)

The documentation on routing is a little fuzzy, as most examples do not follow the defined naming convention for Request/Response DTo's as outlines in the "Your First Web Service Explained"

I have also read this SO Post, but the I am left with the same questions after following the advice.


回答1:


I cant imagine if you have a service with 4+ methods, that your really need to make 4+ service classes, 4+ Request DTO's and 4+ response DTO's.

No you need 1 service class with 4 methods - each method of course taking a request Dto and returning a Response Dto. Obviously for some methods you might not need a response Dto

For example, here's how a typical service with 5 methods might look like:

public class ProductsService: Service
{
    [Route("/products")]
    public class GetProductsRequest: IReturn<List<ProductResponse>> {}

    public class ProductResponse
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public object Get(GetProductsRequest request)
    {
        IEnumerable<ProductResponse> response = ...
        return response;
    }

    [Route("/products/{id}")]
    public class GetProductRequest: IReturn<ProductResponse> 
    {
        public int Id { get; set; }
    }

    public object Get(GetProductRequest request)
    {
        ProductResponse response = ...
        return response;
    }

    [Route("/products")]
    public class CreateProductRequest 
    {
        public string Name { get; set; }
    }

    public object Put(CreateProductRequest request)
    {
        ... create the product here
        return new HttpResult { StatusCode = HttpStatusCode.Created };
    }

    [Route("/products/{id}")]
    public class UpdateProductRequest
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public object Patch(UpdateProductRequest request)
    {
        ... update the product here
        return new HttpResult { StatusCode = HttpStatusCode.Accepted };
    }

    [Route("/products/{id}")]
    public class DeleteProductRequest
    {
        public int Id { get; set; }
    }

    public object Delete(DeleteProductRequest request)
    {
        ... delete the product here
        return new HttpResult { StatusCode = HttpStatusCode.Accepted };
    }
}

I've put the Request and Response DTOs as nested classes to the service, but it would make it more readable to have them in separate files:

public class ProductsService: Service
{
    public object Get(GetProductsRequest request)
    {
        IEnumerable<ProductResponse> response = ...
        return response;
    }

    public object Get(GetProductRequest request)
    {
        ProductResponse response = ...
        return response;
    }

    public object Put(CreateProductRequest request)
    {
        ... create the product here
        return new HttpResult { StatusCode = HttpStatusCode.Created };
    }

    public object Patch(UpdateProductRequest request)
    {
        ... update the product here
        return new HttpResult { StatusCode = HttpStatusCode.Accepted };
    }

    public object Delete(DeleteProductRequest request)
    {
        ... delete the product here
        return new HttpResult { StatusCode = HttpStatusCode.Accepted };
    }
}


来源:https://stackoverflow.com/questions/14086763/servicestack-crud-service-routing-documentation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!