AutoQuery
is a powerful library for .NET that simplifies dynamic query building, filtering, and pagination using expression trees. It provides a flexible and extensible way to handle complex query scenarios. The AutoQuery.AspNetCore
package extends this functionality with seamless integration into ASP.NET Core applications, offering middleware support and field projection capabilities.
- Dynamic Query Building: Generate queries dynamically using expression trees.
- Filtering: Apply flexible filtering logic to refine query results.
- Field Projection: Return only the specified fields to optimize API responses.
- Pagination and Sorting: Built-in support for pagination and sorting.
- ASP.NET Core Integration: Middleware support for easy integration into ASP.NET Core projects.
The benchmark results provide an overview of AutoQuery
's performance in handling dynamic queries and filtering. These results can help evaluate its suitability for various application scenarios.
Install the core library via NuGet:
dotnet add package AutoQuery
For ASP.NET Core integration, install the extension package:
dotnet add package AutoQuery.AspNetCore
- Define a query options class implementing the
IQueryOptions
interface:public class UserQueryOptions : IQueryPagedOptions { public int[]? FilterIds { get; set; } public string? FilterName { get; set; } public string? Fields { get; set; } public string? Sort { get; set; } public int? Page { get; set; } public int? PageSize { get; set; } }
- Configure the filter logic by implementing
IFilterQueryConfiguration
:public class UserQueryConfiguration : IFilterQueryConfiguration<UserQueryOptions, User> { public void Configure(FilterQueryBuilder<UserQueryOptions, User> builder) { builder.Property(q => q.FilterIds, d => d.Id) .HasCollectionContains(); builder.Property(q => q.FilterName, d => d.Name) .HasEqual(); } }
- Use
AutoQuery
to process queries:var queryProcessor = new QueryProcessor(); queryProcessor.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly()); var users = new List<User> { new User(4, "Bob Brown", "bob.brown@example.com", new DateTime(1988, 12, 30)), new User(1, "John Doe", "john.doe@example.com", new DateTime(1990, 1, 1)), new User(3, "Alice Johnson", "alice.johnson@example.com", new DateTime(1992, 8, 23)), new User(5, "Charlie Davis", "charlie.davis@example.com", new DateTime(1995, 3, 10)), new User(2, "Jane Smith", "jane.smith@example.com", new DateTime(1985, 5, 15)), }; var result = users.AsQueryable().ApplyQuery(queryProcessor, new UserQueryOptions() { Fields = "Id,Name", Sort = "-Id", FilterIds = new[] { 3, 4 }, FilterName = "Alice Johnson" }).ToArray(); Console.WriteLine("Filtered Users:"); foreach (var user in result) { Console.WriteLine($"{user.Id}: {user.Name}"); }
- Example Output:
Filtered Users: 3: Alice Johnson
- Define a query options class implementing the
IQueryOptions
interface:public class UserQueryOptions : IQueryPagedOptions { [FromQuery(Name = "filter[ids]")] public int[]? FilterIds { get; set; } [FromQuery(Name = "filter[name]")] public string? FilterName { get; set; } [FromQuery(Name = "fields")] public string? Fields { get; set; } [FromQuery(Name = "sort")] public string? Sort { get; set; } [FromQuery(Name = "page")] public int? Page { get; set; } [FromQuery(Name = "pageSize")] public int? PageSize { get; set; } }
- Configure the filter logic by implementing
IFilterQueryConfiguration
:public class UserQueryConfiguration : IFilterQueryConfiguration<UserQueryOptions, User> { public void Configure(FilterQueryBuilder<UserQueryOptions, User> builder) { builder.Property(q => q.FilterIds, d => d.Id) .HasCollectionContains(); builder.Property(q => q.FilterName, d => d.Name) .HasEqual(); } }
- Register the required services in
Program.cs
:// Add AutoQuery services builder.Services.AddAutoQuery(Assembly.GetEntryAssembly());
- Create a controller to handle queries:
[ApiController] [Route("[controller]")] public class UsersController : ControllerBase { private readonly IQueryProcessor _queryProcessor; private List<User> users = new List<User> { new User(4, "Bob Brown", "bob.brown@example.com", new DateTime(1988, 12, 30)), new User(1, "John Doe", "john.doe@example.com", new DateTime(1990, 1, 1)), new User(3, "Alice Johnson", "alice.johnson@example.com", new DateTime(1992, 8, 23)), new User(5, "Charlie Davis", "charlie.davis@example.com", new DateTime(1995, 3, 10)), new User(2, "Jane Smith", "jane.smith@example.com", new DateTime(1985, 5, 15)), }; public UsersController(IQueryProcessor queryProcessor) { _queryProcessor = queryProcessor; } [HttpGet] [EnableFieldProjection] public IActionResult Get(UserQueryOptions queryOptions) { var result = users.AsQueryable() .ApplyQueryPagedResult(_queryProcessor, queryOptions); return Ok(result); } }
- Example Request:
GET /Users?filter[ids]=1&filter[ids]=3&fields=Id,Name&sort=-Id&page=1&pageSize=2
- Example Response:
{
"datas": [
{
"id": 3,
"name": "Alice Johnson"
},
{
"id": 1,
"name": "John Doe"
}
],
"count": 2,
"totalPages": 1,
"page": 1
}
Contributions are welcome! Feel free to submit issues or pull requests to improve the project.
This project is licensed under the MIT License.