如何使用AutoQuery执行更复杂的查询

时间:2014-09-23 23:49:16

标签: servicestack ormlite-servicestack

从ServiceStack端点给出以下定义:

public class LoanQueue
{
    public int LoanId { get; set; }
    public DateTime Submitted { get; set; }
    public DateTime Funded { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Fico { get; set; }
    public int Fraud { get; set; }
    public int CDS { get; set; }
    public int IDA { get; set; }
    public string Income { get; set; }
    public string Liabilities { get; set; }
    public string Agent { get; set; }
    public string Status { get; set; }
    public string State { get; set; }
    public string Product { get; set; }
    public string Comment { get; set; }
}

public enum DateType
{
    None,
    Submitted,
    Funded
}

[Route("/loan/queue/search", "GET")]
public class LoanQueueQueryGet : QueryBase<LoanQueue>
{
    public DateType DateType { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public string AgentUserName { get; set; }
    public Languange Languange { get; set; }
    public bool WorkingLoan { get; set; }
    public bool MicrobusinessLoan { get; set; }
    public LoanStatus LoanStatus { get; set; }
}

public object Get(LoanQueueQueryGet request)
{
    if (request == null) throw new ArgumentNullException("request");
    var profiler = Profiler.Current;
    using (profiler.Step("LoanServices.LoanQueue"))
    {

        SqlExpression<LoanQueue> q = AutoQuery.CreateQuery(request, Request.GetRequestParams());

        QueryResponse<LoanQueue> loanQueueResponse = AutoQuery.Execute(request, q);

        return loanQueueResponse;
    }
}

我的问题是,“甚至可以根据服务impl中的请求对象运行条件逻辑”?例如

如果DateType == DateType.Submitted

然后使用BETWEEN子句(StartDate / EndDate)或

查询LoanQueue上的已提交属性

如果DateType == DateType.Funded

然后使用BETWEEN子句(StartDate / EndDate)查询LoanQueue上的资助属性。

我的猜测是,我试图将AutoQuery过度弯曲,并且只是以旧时尚的方式进行编码会更好。我非常喜欢AutoQuery插件的内置功能,我确信有时它会满足我的需求。

谢谢你, 斯蒂芬

1 个答案:

答案 0 :(得分:1)

AutoQuery会忽略所有不匹配的字段,因此您可以使用它们来扩展已填充的AutoQuery以及其他自定义逻辑,例如:

public object Get(LoanQueueQueryGet request)
{
    var q = AutoQuery.CreateQuery(request, Request.GetRequestParams());

    if (request.DateType == DateType.Submitted)
    {
        q.And(x => x.Submitted >= request.StartDate && x.Submitted < request.EndDate);
    }
    else
    {
        q.And(x => x.Funded >= request.StartDate && x.Funded < request.EndDate);
    }

    var loanQueueResponse = AutoQuery.Execute(request, q);
    return loanQueueResponse;
}