Skip to content
lukelukic edited this page Jul 23, 2020 · 4 revisions

Intro

The idea behind quryable builder is very simple - it should reduce the amount of boilerplate query code in your codebase. It is designed around the idea of query object and tries to further automate the process. You should simply design a class that will server as a query object, decorate it with some of the predifined attributes, and, if needed, inherit from our base clasess and let the builder to the heavy lifting of query composition.

Quick example

Let's say you have a user storage you would like to query and it's exposed via the IQueryable interface.

First, you have your user class:

public class TestUser
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string Username { get; set; }
    public int Age { get; set; }
}

Then, you should define your query object class:

public class UserQuery 
{
    [QueryProperty]
    public int? Age { get; set; }
    [QueryProperties(ComparisonOperator.Contains, "FirstName", "Username")]
    public string Keyword { get; set; }
}

And finally, you query your datasource:

var queryObject = new UserQuery 
{ 
    Age = 20; 
    Keyword = "ma"
}

var result = yourQueryable.BuildQuery(queryObject);

The resulting dataset will consist of all the users having being exactly 20 years old and containing string "ma" (case-insensitive) in either their Username of FirstName properties.

Clone this wiki locally