ServiceStack ORMLite - How to Select All to match the request DTO's properties automatically

↘锁芯ラ 提交于 2019-12-11 13:54:35

问题


I have several ServiceStack ORMLite POCO, one is Company below.

    public class Company
    {
        [AutoIncrement]
        public int id { get; set; }
        public string company { get; set; }
        public int? companyNo { get; set; }
        public bool? active { get; set; }
    }

If two properties are valid in the following request: req.company="ABC Company", req.active=ture, and all other properties are null. Then it can return all records matching the two properties. The code may look like below:

    public object Get(Company req)
    {
        return Db.Select<Company>().Where<Company>(req);
    }

Does ServiceStack ORMLite have such a WHRER to auto-match the valid properties in the request DTO?


回答1:


This is not a feature in OrmLite, but it's available in AutoQuery where you just need to define the Request DTO you want to query, e.g:

[Route("/company/search")]
public class QueryCompany : IQuery<Company>
{
    public int Id { get; set; }
    public string Company { get; set; }
    public int? CompanyNo { get; set; }
    public bool? Active { get; set; }
}

With just the Request DTO, ServiceStack automatically creates the Service for you which you can query like any other Service.

Enable AutoQuery

You can enable AutoQuery by registering the AutoQuery Feature, e.g:

Plugins.Add(new AutoQueryFeature { MaxLimit = 100 });

AutoQuery is available in the ServiceStack.Server NuGet package:

PM> Install-Package ServiceStack.Server



回答2:


Thanks mythz. It works for me. My code is like below:

// ====== 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 });
    //...
}

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

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) 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?



来源:https://stackoverflow.com/questions/25557688/servicestack-ormlite-how-to-select-all-to-match-the-request-dtos-properties-a

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