|
2 | 2 |
|
3 | 3 | [](https://github.com/willysoft/AutoQuery/actions) [](https://github.com/willysoft/AutoQuery/actions) [](https://github.com/willysoft/AutoQuery/releases) [](https://www.nuget.org/packages/AutoQuery/) [](https://www.nuget.org/packages/AutoQuery.AspNetCore/) |
4 | 4 |
|
5 | | -`AutoQuery` is a middleware and toolset for ASP.NET Core that enables filtering HTTP requests based on the local port. This package is ideal for scenarios where certain operations must be restricted to specific ports, enhancing application security and traffic control. |
| 5 | +`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. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- **Dynamic Query Building**: Generate queries dynamically using expression trees. |
| 10 | +- **Filtering**: Apply flexible filtering logic to refine query results. |
| 11 | +- **Field Projection**: Return only the specified fields to optimize API responses. |
| 12 | +- **Pagination and Sorting**: Built-in support for pagination and sorting. |
| 13 | +- **ASP.NET Core Integration**: Middleware support for easy integration into ASP.NET Core projects. |
6 | 14 |
|
7 | 15 | ## Installation |
8 | 16 |
|
9 | | -Install the package via NuGet: |
| 17 | +### AutoQuery |
| 18 | + |
| 19 | +Install the core library via NuGet: |
10 | 20 |
|
11 | 21 | ```bash |
12 | 22 | dotnet add package AutoQuery |
13 | 23 | ``` |
14 | 24 |
|
| 25 | +### AutoQuery.AspNetCore |
| 26 | + |
| 27 | +For ASP.NET Core integration, install the extension package: |
| 28 | + |
| 29 | +```bash |
| 30 | +dotnet add package AutoQuery.AspNetCore |
| 31 | +``` |
| 32 | + |
15 | 33 | ## Getting Started |
16 | 34 |
|
| 35 | +### Using AutoQuery in .NET Applications |
| 36 | + |
| 37 | +1. Define a query options class implementing the `IQueryOptions` interface: |
| 38 | + ```csharp |
| 39 | + public class UserQueryOptions : IQueryPagedOptions |
| 40 | + { |
| 41 | + public int[]? FilterIds { get; set; } |
| 42 | + public string? FilterName { get; set; } |
| 43 | + public string? Fields { get; set; } |
| 44 | + public string? Sort { get; set; } |
| 45 | + public int? Page { get; set; } |
| 46 | + public int? PageSize { get; set; } |
| 47 | + } |
| 48 | + ``` |
| 49 | +2. Configure the filter logic by implementing `IFilterQueryConfiguration`: |
| 50 | + ```csharp |
| 51 | + public class UserQueryConfiguration : IFilterQueryConfiguration<UserQueryOptions, User> |
| 52 | + { |
| 53 | + public void Configure(FilterQueryBuilder<UserQueryOptions, User> builder) |
| 54 | + { |
| 55 | + builder.Property(q => q.FilterIds, d => d.Id) |
| 56 | + .HasCollectionContains(); |
| 57 | + builder.Property(q => q.FilterName, d => d.Name) |
| 58 | + .HasEqual(); |
| 59 | + } |
| 60 | + } |
| 61 | + ``` |
| 62 | +3. Use `AutoQuery` to process queries: |
| 63 | + ```csharp |
| 64 | + var queryProcessor = new QueryProcessor(); |
| 65 | + queryProcessor.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly()); |
| 66 | + |
| 67 | + var users = new List<User> |
| 68 | + { |
| 69 | + new User( 4, "Bob Brown", "[email protected]", new DateTime( 1988, 12, 30)), |
| 70 | + new User( 1, "John Doe", "[email protected]", new DateTime( 1990, 1, 1)), |
| 71 | + new User( 3, "Alice Johnson", "[email protected]", new DateTime( 1992, 8, 23)), |
| 72 | + new User( 5, "Charlie Davis", "[email protected]", new DateTime( 1995, 3, 10)), |
| 73 | + new User( 2, "Jane Smith", "[email protected]", new DateTime( 1985, 5, 15)), |
| 74 | + }; |
| 75 | + |
| 76 | + var result = users.AsQueryable().ApplyQuery(queryProcessor, new UserQueryOptions() |
| 77 | + { |
| 78 | + Fields = "Id,Name", |
| 79 | + Sort = "-Id", |
| 80 | + FilterIds = new[] { 3, 4 }, |
| 81 | + FilterName = "Alice Johnson" |
| 82 | + }).ToArray(); |
| 83 | + |
| 84 | + Console.WriteLine("Filtered Users:"); |
| 85 | + foreach (var user in result) |
| 86 | + { |
| 87 | + Console.WriteLine($"{user.Id}: {user.Name}"); |
| 88 | + } |
| 89 | + ``` |
| 90 | + 4. Example Output: |
| 91 | + ```plaintext |
| 92 | + Filtered Users: |
| 93 | + 4: Bob Brown |
| 94 | + 3: Alice Johnson |
| 95 | + ``` |
| 96 | + |
| 97 | +### Using AutoQuery.AspNetCore in ASP.NET Core Applications |
| 98 | + |
| 99 | +1. Define a query options class implementing the `IQueryOptions` interface: |
| 100 | + ```csharp |
| 101 | + public class UserQueryOptions : IQueryPagedOptions |
| 102 | + { |
| 103 | + [FromQuery(Name = "filter[ids]")] |
| 104 | + public int[]? FilterIds { get; set; } |
| 105 | + [FromQuery(Name = "filter[name]")] |
| 106 | + public string? FilterName { get; set; } |
| 107 | + [FromQuery(Name = "fields")] |
| 108 | + public string? Fields { get; set; } |
| 109 | + [FromQuery(Name = "sort")] |
| 110 | + public string? Sort { get; set; } |
| 111 | + [FromQuery(Name = "page")] |
| 112 | + public int? Page { get; set; } |
| 113 | + [FromQuery(Name = "pageSize")] |
| 114 | + public int? PageSize { get; set; } |
| 115 | + } |
| 116 | + ``` |
| 117 | +2. Configure the filter logic by implementing `IFilterQueryConfiguration`: |
| 118 | + ```csharp |
| 119 | + public class UserQueryConfiguration : IFilterQueryConfiguration<UserQueryOptions, User> |
| 120 | + { |
| 121 | + public void Configure(FilterQueryBuilder<UserQueryOptions, User> builder) |
| 122 | + { |
| 123 | + builder.Property(q => q.FilterIds, d => d.Id) |
| 124 | + .HasCollectionContains(); |
| 125 | + builder.Property(q => q.FilterName, d => d.Name) |
| 126 | + .HasEqual(); |
| 127 | + } |
| 128 | + } |
| 129 | + ``` |
| 130 | +3. Register the required services in `Program.cs`: |
| 131 | + ```csharp |
| 132 | + // Add AutoQuery services |
| 133 | + builder.Services.AddAutoQuery(Assembly.GetEntryAssembly()); |
| 134 | + ``` |
| 135 | +4. Create a controller to handle queries: |
| 136 | + ```csharp |
| 137 | + [ApiController] |
| 138 | + [Route("[controller]")] |
| 139 | + public class UsersController : ControllerBase |
| 140 | + { |
| 141 | + private readonly IQueryProcessor _queryProcessor; |
| 142 | + |
| 143 | + private List<User> users = new List<User> |
| 144 | + { |
| 145 | + new User( 4, "Bob Brown", "[email protected]", new DateTime( 1988, 12, 30)), |
| 146 | + new User( 1, "John Doe", "[email protected]", new DateTime( 1990, 1, 1)), |
| 147 | + new User( 3, "Alice Johnson", "[email protected]", new DateTime( 1992, 8, 23)), |
| 148 | + new User( 5, "Charlie Davis", "[email protected]", new DateTime( 1995, 3, 10)), |
| 149 | + new User( 2, "Jane Smith", "[email protected]", new DateTime( 1985, 5, 15)), |
| 150 | + }; |
| 151 | + |
| 152 | + public UsersController(IQueryProcessor queryProcessor) |
| 153 | + { |
| 154 | + _queryProcessor = queryProcessor; |
| 155 | + } |
| 156 | + |
| 157 | + [HttpGet] |
| 158 | + [EnableFieldProjection] |
| 159 | + public IActionResult Get(UserQueryOptions queryOptions) |
| 160 | + { |
| 161 | + var result = users.AsQueryable() |
| 162 | + .ApplyQueryPaged(_queryProcessor, queryOptions); |
| 163 | + return Ok(result); |
| 164 | + } |
| 165 | + } |
| 166 | + ``` |
| 167 | +5. Example Request: |
| 168 | +```http |
| 169 | +GET /Users?filter[ids]=1&filter[ids]=3&fields=Id,Name&sort=-Id&page=1&pageSize=2 |
| 170 | +``` |
| 171 | +6. Example Response: |
| 172 | +```json |
| 173 | +{ |
| 174 | + "datas": [ |
| 175 | + { |
| 176 | + "id": 3, |
| 177 | + "name": "Alice Johnson" |
| 178 | + }, |
| 179 | + { |
| 180 | + "id": 1, |
| 181 | + "name": "John Doe" |
| 182 | + } |
| 183 | + ], |
| 184 | + "count": 2, |
| 185 | + "totalPages": 1, |
| 186 | + "page": 1 |
| 187 | +} |
| 188 | +``` |
| 189 | + |
17 | 190 | ## Contribution |
18 | 191 |
|
19 | 192 | Contributions are welcome! Feel free to submit issues or pull requests to improve the project. |
|
0 commit comments