The idea behind quryable builder is simple - to 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 serve 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.
In it's essence, it is a collection of extension methods for predicate building, sorting and paging on top of generic IQueryable interface. Based on your use-case, there are several options to choose from.
Nuget Package Doublel.QueryableBuilder
Install-Package Doublel.QueryableBuilder
OR
dotnet add package Doublel.QueryableBuilder
Minimum Requirements: .NET Standard 2.0
The tool was initialy designed for company's internal purposes, but we've figured out it could help someone else as well. If the example clicks for you, proceed to building some queryes.
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 users = new List<User> { /* a bunch of users */}.AsQueryable();
var result = users.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.