ServiceStack:URL 中指定的基于上下文的路由是否可行?
ServiceStack: Is context based routing specified in the URL possible?
我希望保留我以前使用 OData 服务但通过 ServiceStack 公开的服务层代码库中曾经拥有的大量功能,假设我实现了服务逻辑,我不想当这基本上是我想要实现的目标时,必须为请求制作大量新的 DTO,除非框架 "forces" 我声明一堆额外的 classes 而没有功能性增益 ...
[Route("~/{Type}")]
public class GetRequest
{
public string Type {get; set; }
public string Select { get; set; }
public string Expand { get; set; }
public string Filter { get; set; }
public string GroupBy { get; set; }
public string OrderBy { get; set; }
}
public ServiceBase<T> : Service
{
public virtual IEnumerable<T> Get(GetRequest<T> request) { ... }
}
public FooService : ServiceBase<Foo>
{
public override IEnumerable<Foo> Get(GetRequest<Foo> request) { ... }
}
我能看到的唯一其他实现方法是基本上必须创建一个 FooRequest DTO,它继承自这里的通用 DTO,不添加任何内容。
虽然在某些情况下可能会出现这种情况,但对于我必须迁移的数百个端点中的大部分来说,这似乎很浪费,而且可能需要我不得不生成代码,Service Stack 声称 "isn't needed".
我的情况变得更糟了,因为我 "multiple data contexts" 需要考虑例如...
// base implementation for all services, derives from ServiceStack Service
public abstract class ServiceBase<T> : Service { ... }
// core service then one concrete implementation off that
public class CoreService<T> : ServiceBase<T> { ... }
public CoreFooService : CoreService<Foo> { ... }
/// b2b service then one concrete implementation off of that
public class B2BService<T> : ServiceBase<T> { ... }
public class BarB2BService : B2BService<Bar> { ... }
...使用基于 OData 的实现,我只需要添加每个新的 class 即可为堆栈中的该类型数据添加自定义点。
使用 ServiceStack,这似乎仍然有可能关于服务 classes(我想,但我不清楚路由是如何工作的)...我感到困惑的地方是理解请求 DTO在所有 get 请求中基本相同,但根据 URL.
中的某些 tpye 信息,似乎无法路由
理想情况下,我想通过使用的 HTTP 动词的组合将标准请求 DTO 路由到服务方法,然后 [Route("~/{Context}/{Type}")] 在 url(这是 DTO 上的属性用法)。
我感觉 ServiceStack 不能像这样工作,并且需要我为每个服务上的每个方法定义一个新的 DTO,我将不得不定义一堆新服务不存在,其中没有新的实现细节,只是为了满足框架的需要。
或者我是否遗漏了一些关于如何使用此处的框架来避免这项工作的技巧?
你可以有多个服务基础 类 但你的请求 DTO 不能是通用的,它必须是一个具体的请求 DTO,但它可以继承基础 类,例如全部AutoQuery RDBMS Services inherit from QueryDb<T>
or QueryDb.
您的路线应以 /
开头(即不是 ~/
)并且您可以有一个接受任何类型的参数:
[Route("/data/{Type}")]
public class GetData
{
public string Type {get; set; }
public string Select { get; set; }
public string Expand { get; set; }
public string Filter { get; set; }
public string GroupBy { get; set; }
public string OrderBy { get; set; }
}
可以调用:
GET /data/Anything
但是您的服务应该具有相同的 return 类型(即遵守其服务合同),因此通配符服务不会有用,除非您 return 相同的非结构化数据响应,例如 Dictionary<string,object>
、List<object>
、等等
I get the feeling though that ServiceStack doesn't work like this and is going to require me to define a new DTO for literally every method on every service and i'm going to have to define a bunch of new services that don't exist with no new implementation details in them just to satisfy the frameworks needs.
是的 ServiceStack 要求每个服务都由其 Request DTO 定义,它是描述该服务合同的主权限。这不仅仅是安抚框架的要求,Request DTO is the message that invokes a Service, which is the only thing generic Service Clients need to send 调用一个服务,如果它不存在就不能发送,它也不能有一个 Typed API(没有代码- gen) 如果没有类型。
我希望保留我以前使用 OData 服务但通过 ServiceStack 公开的服务层代码库中曾经拥有的大量功能,假设我实现了服务逻辑,我不想当这基本上是我想要实现的目标时,必须为请求制作大量新的 DTO,除非框架 "forces" 我声明一堆额外的 classes 而没有功能性增益 ...
[Route("~/{Type}")]
public class GetRequest
{
public string Type {get; set; }
public string Select { get; set; }
public string Expand { get; set; }
public string Filter { get; set; }
public string GroupBy { get; set; }
public string OrderBy { get; set; }
}
public ServiceBase<T> : Service
{
public virtual IEnumerable<T> Get(GetRequest<T> request) { ... }
}
public FooService : ServiceBase<Foo>
{
public override IEnumerable<Foo> Get(GetRequest<Foo> request) { ... }
}
我能看到的唯一其他实现方法是基本上必须创建一个 FooRequest DTO,它继承自这里的通用 DTO,不添加任何内容。
虽然在某些情况下可能会出现这种情况,但对于我必须迁移的数百个端点中的大部分来说,这似乎很浪费,而且可能需要我不得不生成代码,Service Stack 声称 "isn't needed".
我的情况变得更糟了,因为我 "multiple data contexts" 需要考虑例如...
// base implementation for all services, derives from ServiceStack Service
public abstract class ServiceBase<T> : Service { ... }
// core service then one concrete implementation off that
public class CoreService<T> : ServiceBase<T> { ... }
public CoreFooService : CoreService<Foo> { ... }
/// b2b service then one concrete implementation off of that
public class B2BService<T> : ServiceBase<T> { ... }
public class BarB2BService : B2BService<Bar> { ... }
...使用基于 OData 的实现,我只需要添加每个新的 class 即可为堆栈中的该类型数据添加自定义点。
使用 ServiceStack,这似乎仍然有可能关于服务 classes(我想,但我不清楚路由是如何工作的)...我感到困惑的地方是理解请求 DTO在所有 get 请求中基本相同,但根据 URL.
中的某些 tpye 信息,似乎无法路由理想情况下,我想通过使用的 HTTP 动词的组合将标准请求 DTO 路由到服务方法,然后 [Route("~/{Context}/{Type}")] 在 url(这是 DTO 上的属性用法)。
我感觉 ServiceStack 不能像这样工作,并且需要我为每个服务上的每个方法定义一个新的 DTO,我将不得不定义一堆新服务不存在,其中没有新的实现细节,只是为了满足框架的需要。
或者我是否遗漏了一些关于如何使用此处的框架来避免这项工作的技巧?
你可以有多个服务基础 类 但你的请求 DTO 不能是通用的,它必须是一个具体的请求 DTO,但它可以继承基础 类,例如全部AutoQuery RDBMS Services inherit from QueryDb<T>
or QueryDb.
您的路线应以 /
开头(即不是 ~/
)并且您可以有一个接受任何类型的参数:
[Route("/data/{Type}")]
public class GetData
{
public string Type {get; set; }
public string Select { get; set; }
public string Expand { get; set; }
public string Filter { get; set; }
public string GroupBy { get; set; }
public string OrderBy { get; set; }
}
可以调用:
GET /data/Anything
但是您的服务应该具有相同的 return 类型(即遵守其服务合同),因此通配符服务不会有用,除非您 return 相同的非结构化数据响应,例如 Dictionary<string,object>
、List<object>
、等等
I get the feeling though that ServiceStack doesn't work like this and is going to require me to define a new DTO for literally every method on every service and i'm going to have to define a bunch of new services that don't exist with no new implementation details in them just to satisfy the frameworks needs.
是的 ServiceStack 要求每个服务都由其 Request DTO 定义,它是描述该服务合同的主权限。这不仅仅是安抚框架的要求,Request DTO is the message that invokes a Service, which is the only thing generic Service Clients need to send 调用一个服务,如果它不存在就不能发送,它也不能有一个 Typed API(没有代码- gen) 如果没有类型。