我正在使用ServiceStack将我的厚客户端连接到我们的API服务器,我非常喜欢它。但是,我发现必须为每个请求(Foo、FooResponse和FooService)编写三个类才会有点麻烦。
在我的项目中,我有很多DAO接口,它们如下所示:
public interface ICustomerDao {
public IList<Customer> FindCustomers(long regionId, string keyword);
}我希望能说出这样的话:
public interface ICustomerDao {
[AutoApi("Loads customers for the given region whose names match the keyword")]
[AutoRoute("/search/customer/{regionId}/{keyword}"]
public IList<Customer> FindCustomers(long regionId, string keyword);
}
public class SomeBusinessLogic {
[AutoService(typeof(ICustomerDao))]
public IList<Customer> FindCustomers(long regionId, string keyword) {
// lots of business logic here
}
}然后,我希望为我自动生成以下类:
FindCustomers:ServiceStack DTO请求FindCustomersResponse:回应FindCustomersService:接受FindCustomers DTO的服务,然后调用SomeBusinessLogic.FindCustomers(req.RegionId, req.Keyword)并将其返回值封装在FindCustomersResponse中ApiServiceCustomerDao:通过自动生成方法实现ICustomerDao,这些方法构造FooRequest并与适当的服务联系,然后接收FooResponse并自动解压它。像这样的东西已经存在了吗?如果没有,执行起来会有多难?有更好的办法吗?
发布于 2015-06-06 13:47:37
首先,我建议您看看AutoQuery是否适合快速创建数据驱动服务。
动态生成和注册服务
由于ServiceStack促进了一个代码优先开发模型,并且您的请求和响应DTO代表了您的服务契约,我强烈建议您不要尝试动态生成它们,因为您应该保留对其定义的完全控制,但是您可以使用它们作为模板,通过遵循相同的方法AutoQuery用于生成服务实现并动态注册它们来动态生成您自己的服务实现。
在AutoQuery中,只需定义请求DTO,服务实现的其余部分就会动态生成和注册。由于它返回标准的QueryResponse<T>响应类型,所以没有动态生成DTO的DTO类型,并且由于代码第一请求DTO、客户端仍然保留端到端类型的API。,例如:
var movies = client.Get(new FindMovies { Ratings = new[]{"G","PG-13"} });https://stackoverflow.com/questions/30680397
复制相似问题