简体   繁体   English

ServiceStack遍历所有请求/响应DTO

[英]ServiceStack iterate through all request/response DTO

How can I iterate through all request/response DTOs that are setup with a route? 如何遍历通过路由设置的所有请求/响应DTO?

For example a route like this: 例如这样的路线:

[Route("/api/something", "GET")]
public class SomethingGetRequest : IReturn<List<Something>>
{
    public int SomethingId { get; set; }
}

and Response DTO like this: 和响应DTO像这样:

public class Something
{
    public string A { get; set; }
    public int B { get; set; }
}

I want to have a service action where I can iterate through all setup routes, and retrieve the: 我想进行一项服务操作,在其中可以遍历所有设置路径,并检索以下内容:

  • Request URL 要求网址
  • Class name 班级名称
  • Request properties 请求属性
  • Response DTO properties 响应DTO属性

Is there some Servicstack built in way to do this? 有一些内置的服务堆栈来做到这一点吗?

In the end I would like to use this to autogenerate ExtJS Store/Models. 最后,我想用它来自动生成ExtJS商店/模型。 I'm also interested in a better/alternative approach. 我也对更好/替代方法感兴趣。

EDIT: 编辑:

Here is the solution I came up with: gist 这是我想出的解决方案: 要点

On the /extjs/javascript route the service returns extjs store/models and on the /extjs/metadata route the service returns a zip file with a store/model folder you can drop in a newly created Sencha Architect project. /extjs/javascript路由上,该服务返回extjs商店/模型,在/extjs/metadata路由上,该服务返回一个带有store / model文件夹的zip文件,您可以将其放入新创建的Sencha Architect项目中。 The templates are based on what Architect v. 3.0.1.1343 produces. 模板基于Architect v。3.0.1.1343生成的内容。

This assumes a route decorated like this: 这假定路由装饰如下:

[Route("/api/something", "GET", Summary="route description")]
public class SomethingGetRequest : IReturn<List<Something>>
{

    [ApiMember(Name = "SomethingId", ParameterType = "query", IsRequired = true, DataType = "int")]
    public string SomethingId { get; set; }

}

The current implementation only works for simple properties in request/response DTOs. 当前实现仅适用于请求/响应DTO中的简单属性。 There is room for improvement... 有改进的余地...

Have you looked at the ServiceStack.Api.Swagger package (documented here )? 您是否看过ServiceStack.Api.Swagger包( 在此处记录 )? That sounds pretty similar to what you want. 这听起来很像您想要的。

If the Swagger UI won't work for you out of the box, you could use the ServiceStack.Api.Swagger package and send requests to /resources and its related URLs to get all the metadata in JSON format and then process that data according to your needs. 如果Swagger UI无法立即为您工作,则可以使用ServiceStack.Api.Swagger包并将请求发送到/resources及其相关的URL,以获取JSON格式的所有元数据,然后根据您的需求。 Or if you want to write something in C#, consult the code for SwaggerApiService and SwaggerResourcesService to see how it retrieves all DTOs and routes and extracts metadata about them. 或者,如果您想用C#编写代码 ,请查阅SwaggerApiService和SwaggerResourcesService的代码 ,以了解其如何检索所有DTO和路由并提取有关它们的元数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM