Search code examples
requestservicestackwhere-clausedtoormlite-servicestack

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


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?


Solution

  • 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