ServiceStack AutoQuery - Simplify with Generic and Custom Attributes

user2006633

This question comes from another to Simplify OrmLite with AutoQuery.

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

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?

mythz

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) 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) Dynamically generate your Service entry points

If you need more flexibility you can follow the same approach AutoQuery uses to generate your Services implementations and register them dynamically/


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?

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

As C# doesn't support multiple inheritance your company class would either need to define IQuery properties explicitly or inherit the QueryBase class, e.g:

public class Company : QueryBase { ... }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ServiceStack - Autoquery & OrmLiteCacheClient

From Dev

ServiceStack AutoQuery and [Authenticate] Attribute

From Dev

ServiceStack AutoQuery MVC controller

From Dev

ServiceStack AutoQuery MVC controller

From Dev

ServiceStack AutoQuery, Multiple IJoin

From Dev

ServiceStack AutoQuery MaxLimit

From Dev

Get Custom Attributes Generic

From Dev

ServiceStack - Autoquery & Swapping Client Templates

From Dev

Can you disable count (Total) for ServiceStack AutoQuery?

From Dev

Custom Attributes created inside generic Class

From Dev

Map to custom column names with ServiceStack OrmLite (Without Attributes)

From Dev

ServiceStack.RabbitMq - how to set custom attributes on messages

From Dev

Using ServiceStack Autoquery With Value Types that don't implement IConvertible

From Dev

Simplify generic type inferring

From Dev

Simplify generic type inferring

From Dev

Usage of custom and generic vertex shader attributes in OpenGL and OpenGL ES

From Dev

ServiceStack: Attributes for indexes not working?

From Dev

Dynamically adding attributes in ServiceStack

From Dev

Dynamically adding attributes in ServiceStack

From Dev

.NET simplify complex generic type

From Dev

ServiceStack Custom Registration

From Dev

ServiceStack Custom User Authentication

From Dev

Custom JSON serialization in ServiceStack

From Dev

Custom operator to simplify If-Let

From Dev

Simplify a Wordpress shortcode (or: "custom shortcode")

From Dev

Simplify circular generic constraints in C#?

From Dev

Adding ServiceStack OrmLite attributes in code instead of a property

From Dev

C# trying to simplify Generic Extension Function for Generic Typed Interface

From Dev

C# trying to simplify Generic Extension Function for Generic Typed Interface

Related Related

  1. 1

    ServiceStack - Autoquery & OrmLiteCacheClient

  2. 2

    ServiceStack AutoQuery and [Authenticate] Attribute

  3. 3

    ServiceStack AutoQuery MVC controller

  4. 4

    ServiceStack AutoQuery MVC controller

  5. 5

    ServiceStack AutoQuery, Multiple IJoin

  6. 6

    ServiceStack AutoQuery MaxLimit

  7. 7

    Get Custom Attributes Generic

  8. 8

    ServiceStack - Autoquery & Swapping Client Templates

  9. 9

    Can you disable count (Total) for ServiceStack AutoQuery?

  10. 10

    Custom Attributes created inside generic Class

  11. 11

    Map to custom column names with ServiceStack OrmLite (Without Attributes)

  12. 12

    ServiceStack.RabbitMq - how to set custom attributes on messages

  13. 13

    Using ServiceStack Autoquery With Value Types that don't implement IConvertible

  14. 14

    Simplify generic type inferring

  15. 15

    Simplify generic type inferring

  16. 16

    Usage of custom and generic vertex shader attributes in OpenGL and OpenGL ES

  17. 17

    ServiceStack: Attributes for indexes not working?

  18. 18

    Dynamically adding attributes in ServiceStack

  19. 19

    Dynamically adding attributes in ServiceStack

  20. 20

    .NET simplify complex generic type

  21. 21

    ServiceStack Custom Registration

  22. 22

    ServiceStack Custom User Authentication

  23. 23

    Custom JSON serialization in ServiceStack

  24. 24

    Custom operator to simplify If-Let

  25. 25

    Simplify a Wordpress shortcode (or: "custom shortcode")

  26. 26

    Simplify circular generic constraints in C#?

  27. 27

    Adding ServiceStack OrmLite attributes in code instead of a property

  28. 28

    C# trying to simplify Generic Extension Function for Generic Typed Interface

  29. 29

    C# trying to simplify Generic Extension Function for Generic Typed Interface

HotTag

Archive