servicestack,C#,Automation,servicestack" /> servicestack,C#,Automation,servicestack" />

C# 自动生成服务、其DTO和DAO

C# 自动生成服务、其DTO和DAO,c#,automation,servicestack,C#,Automation,servicestack,我正在使用ServiceStack将我的胖客户端连接到我们的API服务器,我非常喜欢它。然而,我发现必须为每个请求编写三个类(Foo、FooResponse和FooService)有些麻烦 在我的项目中,我有很多DAO接口,看起来像这样: public interface ICustomerDao { public IList<Customer> FindCustomers(long regionId, string keyword); } public interface

我正在使用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
    }
}
公共接口ICCustomerDAO{
公共IList FindCustomers(长regionId,字符串关键字);
}
我想这样说:

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
    }
}
公共接口ICCustomerDAO{
[AutoApi(“加载名称与关键字匹配的给定区域的客户”)]
[自动路由(“/search/customer/{regionId}/{keyword}”]
公共IList FindCustomers(长regionId,字符串关键字);
}
公共类业务逻辑{
[自动服务(类型化(ICCustomerDAO))]
公共IList FindCustomers(长regionId,字符串关键字){
//这里有很多商业逻辑
}
}
然后,我希望为我自动生成以下类:

  • FindCustomers
    :一个ServiceStack DTO请求
  • 查找客户响应
    :响应
  • FindCustomersService
    :一种服务,它接受FindCustomers数据,然后调用
    SomeBusinessLogic.FindCustomers(req.RegionId,req.Keyword)
    ,并将其返回值包装在
    FindCustomersResponse
  • ApiServiceCustomerDao
    :通过自动生成方法实现
    iccustomerdao
    ,这些方法构造FooRequest并联系相应的服务,然后接收FooResponse并自动解包
这样的事情已经存在了吗?如果没有,实施起来有多困难?有没有更好的方法

首先,我建议您看看它是否适合快速创建数据驱动服务

动态生成和注册服务 由于ServiceStack推广代码优先的开发模型,并且您的请求和响应DTO代表您的服务契约,我强烈建议您不要尝试动态生成它们,因为您应该保留对其定义的完全控制,但您可以将它们用作模板,通过以下方式动态生成您自己的服务实现e同样的方法

在AutoQuery中,您只需要定义您的请求DTO,其余的服务实现将动态生成和注册。由于它返回标准的
QueryResponse
响应类型,因此不会动态生成DTO,并且由于代码优先请求DTO,客户端,例如:


我不知道服务堆栈,但您描述的是泛型。如果它们不起作用,因为您使用的工具无法正确序列化请求(如果您还没有准备好放弃这些限制),您可能可以编写一个t4模板,根据某些属性生成有问题的类(例如,您在上文中的建议)AutoQuery听起来不错,但我可以使用它来执行自定义业务逻辑代码,还是仅限于SQL查询和集合查询?@Bugmaster它主要用于创建数据驱动服务,但仍然允许。但是如果您仍然需要更多的控制,您可以查看AutoQuery创建和注册动态se的方法服务。