ServiceStack自动查询

ServiceStack自动查询

本文介绍了ServiceStack自动查询 - 简化与通用和自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题来自另一个Simplify OrmLite与自动查询。

ServiceStack自动查询允许我所有的不同的获取(AKindOfType DTO)共享同一个code,如下图所示:(我有很多模型,如公司,我的两个问题试图进一步简化code)

ServiceStack AutoQuery allows all my different Get(AKindOfType dto) to share the same code, like below: (I have many models, like Company, my two more questions attempt to simplify the code further)

// ====== Model.cs ========
[Route("/company/search")]
public class QueryableCompany : QueryBase<Company>
{
    public int? Id { get; set; }
    public string Company { get; set; }
    public int? CompanyNo { get; set; }
    public bool? Active { get; set; }
}
public class Company
{
    [AutoIncrement]
    public int id { get; set; }
    public string company { get; set; }
    public int companyNo { get; set; }
    public bool active { get; set; }
}
// ====== Service.cs ========
public IAutoQuery AutoQuery { get; set; }

public object Get(QueryableCompanies dto)
{
    var q = AutoQuery.CreateQuery(dto, Request.GetRequestParams());
    var r = AutoQuery.Execute(dto, q);
    return r.Results;
}

// ====== Global.asax.cs ========
public override void Configure(Container container)
{
    //...
    Plugins.Add(new AutoQueryFeature { MaxLimit = 100 });
    //...
}

然后,我有两个问题基础上,code以上。

Then, I have two more questions based on the code above.

1)因为我有很多要求的DTO的,在得到他们的code(QueryableXXX DTO)都是一样的;如何使用一个通用get()方法返回所有不同类型的DTO的,如:

1) Since I have a lot of request DTOs, their code in Get(QueryableXXX dto) is all the same; How can I use a single generic Get() method to return all different types of DTO, like:

public object Get<T>(T dto) where T : IQuery
{
    var q = AutoQuery.CreateQuery(dto, Request.GetRequestParams());
    return AutoQuery.Execute(dto, q).Results;
}

2)在上面的例子公司,类QueryableCompany显得如此的相似类公司,可以自动查询提供一些属性类公司的成员,并避免创建另一个类似QueryableCompany?

2) In the Company example above, class QueryableCompany seems so similar to class Company, can AutoQuery provide some Attributes to class Company's members, and avoid to create another similar QueryableCompany?

推荐答案

您不能调用服务的通用方法签名作为切入点。但是,你的服务可以继承一个公共服务基础类,您的实现可以调用代替,例如:

a) Have your Services share a common base class

You can't have services call a generic method signature as an entry point. But your services can inherit a common Service base Class which your implementation can call instead, e.g:

public class CompanyServices : MyServiceBase
{
    public object Get(QueryCompany request)
    {
        return base.MyQuery(request);
    }
}

B)覆盖自动查询服务

的默认类

有关,如果你希望所有自动查询服务保持不变genericizing你实现另一个选择是提供自己的自定义AutoQueryServiceBase类所有的自动查询服务可以改用,例如:

b) Override the default class for AutoQuery Services

Another option for genericizing your implementations if you want all AutoQuery Services to remain the same is to provide your own Custom AutoQueryServiceBase class which all your AutoQuery Services can use instead, e.g:

public abstract class MyAutoQueryServiceBase : AutoQueryServiceBase
{
    public override object Exec<From>(IQuery<From> dto)
    {
        var q = AutoQuery.CreateQuery(dto, Request.GetRequestParams());
        return AutoQuery.Execute(dto, q).Results;
    }

    public override object Exec<From, Into>(IQuery<From, Into> dto)
    {
        var q = AutoQuery.CreateQuery(dto, Request.GetRequestParams());
        return AutoQuery.Execute(dto, q).Results;
    }
}

您可以再告诉自动查询与使用你的基类,而不是对所有自动查询服务:

You can then tell AutoQuery to use your base class instead for all AutoQuery services with:

Plugins.Add(new AutoQueryFeature {
    AutoQueryServiceBaseType = typeof(MyAutoQueryServiceBase)
});

C)动态生成服务入口点

如果您需要更多的灵活性,你可以按照同样的方法自动查询用来生成您的服务实现并注册动态 /

2)在上面的例子公司,类QueryableCompany显得如此的相似类公司,可以自动查询提供一些属性类公司的成员,并避免创建另一个类似QueryableCompany?

您不能使用相同的POCO为您的请求查询DTO和POCO的DataModel你查询,因为它是为引用您的定义在类定义的类,生成错误例如:

You can't use the same POCO for both your Request Query DTO and the POCO DataModel you're querying since it's a build error to reference the class your defining in your class definition, e.g:

public class Company : IQuery<Company> {}

您可以使用继承来保存特性,虽然我个人小号:

You can use inheritance to save properties although I personally recommend against doing so for Request DTOs:

public class QueryCompany : Company, IQuery<Company> {}

由于C#不支持多重继承贵公司类要么需要明确定义IQUERY属性或继承 QueryBase 类,例如:

public class Company : QueryBase { ... }

这篇关于ServiceStack自动查询 - 简化与通用和自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 19:51