ServiceStack CRUD服务路由文档

时间:2012-12-29 23:15:49

标签: servicestack

我无法找到解释如何使用多种方法在服务堆栈中实现服务的文档(清除或其他)。所有示例都只显示一个方法的服务类。我不能想象如果你有一个4+方法的服务,你真的需要4 +服务类,4 + Request DTO和4+响应DTO。 (As shown here

示例:

具有4种搜索方法的服务(方法(Identifer1),方法2(标识2),方法3(标识3))

基于请求DTO(Service Stack WikiCreating your First Web Service)的记录结构,请求DTO的类属性,控制到服务类的路由,(如果我理解正确,但再次我正在寻找文档或示例)

关于路由的文档有点模糊,因为大多数示例都不遵循请求/响应DTo的定义命名约定作为“Your First Web Service Explained”中的大纲

我也读过这个SO Post,但在遵循建议之后,我仍然留下了同样的问题。

1 个答案:

答案 0 :(得分:5)

  

我无法想象如果你有一个4+方法的服务,你真的需要4 +服务类,4 + Request DTO和4+响应DTO。

不需要1个服务类,包含4个方法 - 每个方法当然都会接受请求Dto并返回Response Dto。显然,对于某些方法,您可能不需要响应Dto

例如,以下是具有5种方法的典型服务的外观:

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

我已将请求和响应DTO作为嵌套类添加到服务中,但是将它们放在单独的文件中会更具可读性:

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