How to perform a more complex query with AutoQuery

霸气de小男生 提交于 2019-12-22 01:03:20

问题


Given the following definitions from a ServiceStack endpoint:

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;
    }
}

My question is this, "Is it even possible to run conditional logic based on the request object in the service impl"? e.g.

If DateType == DateType.Submitted

then query the Submitted property on the LoanQueue with a BETWEEN clause (StartDate/EndDate) or

If DateType == DateType.Funded

then query the Funded property on the LoanQueue with a BETWEEN clause (StartDate/EndDate).

My guess is that I'm trying to bend AutoQuery too far and would be better served just coding it up the old fashion way. I really like the baked-in features of the AutoQuery plugin and I'm sure there will be times when it will suit my needs.

Thank you, Stephen


回答1:


AutoQuery will ignore any unmatched fields so you're able to use them to extend your populated AutoQuery with additional custom logic, e.g:

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;
}


来源:https://stackoverflow.com/questions/26006418/how-to-perform-a-more-complex-query-with-autoquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!