AutoQuery.AspNetCore
1.1.1
dotnet add package AutoQuery.AspNetCore --version 1.1.1
NuGet\Install-Package AutoQuery.AspNetCore -Version 1.1.1
<PackageReference Include="AutoQuery.AspNetCore" Version="1.1.1" />
<PackageVersion Include="AutoQuery.AspNetCore" Version="1.1.1" />
<PackageReference Include="AutoQuery.AspNetCore" />
paket add AutoQuery.AspNetCore --version 1.1.1
#r "nuget: AutoQuery.AspNetCore, 1.1.1"
#addin nuget:?package=AutoQuery.AspNetCore&version=1.1.1
#tool nuget:?package=AutoQuery.AspNetCore&version=1.1.1
AutoQuery
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.
Features
- 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.
Benchmark
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.
Installation
AutoQuery
Install the core library via NuGet:
dotnet add package AutoQuery
AutoQuery.AspNetCore
For ASP.NET Core integration, install the extension package:
dotnet add package AutoQuery.AspNetCore
Getting Started
Using AutoQuery in .NET Applications
- 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
Using AutoQuery.AspNetCore in ASP.NET Core Applications
- 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
}
Contribution
Contributions are welcome! Feel free to submit issues or pull requests to improve the project.
License
This project is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- AutoQuery (>= 1.1.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.