ServiceStack CRUD 服务路由文档 [英] ServiceStack CRUD Service routing Documentation
问题描述
我找不到解释如何使用多种方法在服务堆栈中实现服务的文档(清晰或其他).所有示例都显示了一个仅具有一种方法的服务类.我无法想象如果你有一个有 4 个以上方法的服务,你真的需要制作 4 个以上的服务类、4 个以上的请求 DTO 和 4 个以上的响应 DTO.( 如图所示)
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 )
示例:
具有 4 个搜索方法的服务(method(Identifer1)、method2(identifer2)、method3(identifer3))
A Service with 4 Search methods ( method(Identifer1), method2(identifer2), method3(identifer3))
基于请求 DTO 的文档结构(Service Stack Wiki, 和 创建您的第一个 Web 服务 ),请求 DTO类属性,控制到服务类的路由,(如果我理解正确,但我又在寻找文档或示例)
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)
关于路由的文档有点模糊,因为大多数示例没有遵循您的第一个 Web 服务说明"
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"
我也读过这篇SO Post,但是在遵循建议后,我遇到了同样的问题.
I have also read this SO Post, but the I am left with the same questions after following the advice.
推荐答案
我无法想象,如果您的服务有 4 个以上的方法,您真的需要制作 4 个以上的服务类、4 个以上的请求 DTO 和 4 个以上的响应 DTO.
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.
不,您需要具有 4 个方法的 1 个服务类 - 当然每个方法都接受请求 Dto 并返回响应 Dto.显然,对于某些方法,您可能不需要响应 Dto
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
例如,具有 5 个方法的典型服务可能如下所示:
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 };
}
}
我已将请求和响应 DTO 作为嵌套类放入服务,但将它们放在单独的文件中会使其更具可读性:
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 };
}
}
这篇关于ServiceStack CRUD 服务路由文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!