|
6 | 6 |
|
7 | 7 | ## Overview |
8 | 8 |
|
9 | | -SoftwareOne.Rql is a high-performance implementation of [Resource Query Language (RQL)](https://docs.platform.softwareone.com/developer-resources/rest-api/resource-query-language) for .NET applications. It enables API consumers to efficiently filter, sort, and paginate data with an intuitive and expressive syntax, delivering precise data retrieval capabilities. |
| 9 | +Mpt.Rql is a high-performance implementation of [Resource Query Language (RQL)](https://docs.platform.softwareone.com/developer-resources/rest-api/resource-query-language) for .NET applications. It enables API consumers to efficiently filter, sort, and paginate data with an intuitive and expressive syntax, delivering precise data retrieval capabilities. |
10 | 10 |
|
11 | 11 | ## Features |
12 | 12 |
|
13 | | -- **Advanced Filtering** - Support for complex logical expressions (AND, OR, NOT) |
14 | | -- **Dynamic Sorting** - Multiple fields with ascending/descending options |
15 | | -- **Pagination** - Skip and limit results for efficient data loading |
16 | | -- **Field Selection** - Return only required fields to minimize payload size |
| 13 | +- **Filtering** - Support for complex logical expressions |
| 14 | +- **Sorting** - Multiple fields with ascending/descending options |
| 15 | +- **Projections** - Return only required fields to minimize payload size |
17 | 16 | - **LINQ Integration** - Seamless integration with Entity Framework and other LINQ providers |
18 | 17 | - **Validation** - Comprehensive error handling and input validation |
19 | 18 | - **Performance Optimized** - Efficient query processing for minimal overhead |
@@ -42,6 +41,77 @@ public void ConfigureServices(IServiceCollection services) |
42 | 41 | } |
43 | 42 | ``` |
44 | 43 |
|
| 44 | +### Applying RQL to IQueryable<> |
| 45 | +```csharp |
| 46 | +public class UserQueryBuilder(IRqlQueryable<User> rql) |
| 47 | +{ |
| 48 | + public IQueryable<User> GetUsersOlderThan(IQueryable<User> sourceQuery, int age) |
| 49 | + { |
| 50 | + var request = new RqlRequest |
| 51 | + { |
| 52 | + Filter = $"gt(age,{age})", // Age must be greater than age specified |
| 53 | + Order = "-age", // Order by age desc |
| 54 | + Select = "id,name" // Select id and name |
| 55 | + }; |
| 56 | + |
| 57 | + var response = rql.Transform(sourceQuery, request); |
| 58 | + |
| 59 | + if (response.IsSuccess) |
| 60 | + return response.Query; // Return transformed query |
| 61 | +
|
| 62 | + response.Errors.ForEach(t => { }); // Iterate through transformation errors (optional) |
| 63 | +
|
| 64 | + Console.WriteLine(response.Graph.Print()) // Visualize the decision graph (optional) |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +## Using RQL mapping |
| 70 | +In many projects, developers prefer to keep database entities separate from data transfer objects (DTOs), especially when their structures differ. To support this approach, Mpt.Rql includes built-in mapping functionality, which is enabled by default and relies on name-based matching. For more advanced scenarios, custom mappings can also be defined manually |
| 71 | + |
| 72 | +### Configure RQL |
| 73 | +```csharp |
| 74 | +public void ConfigureServices(IServiceCollection services) |
| 75 | +{ |
| 76 | + services.AddRql(options => |
| 77 | + { |
| 78 | + // Instruct RQL to look for mappers in specific asembly |
| 79 | + options.ScanForMappers(typeof(Program).Assembly); |
| 80 | + }); |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +### Specify mapping |
| 85 | +```csharp |
| 86 | +internal class UserMapper : IRqlMapper<DbUser, User> |
| 87 | +{ |
| 88 | + public void MapEntity(IRqlMapperContext<DbUser, User> context) |
| 89 | + { |
| 90 | + context |
| 91 | + .MapStatic(t => t.Id, t => t.UserId) |
| 92 | + .MapStatic(t => t.Age, t => t.AgeInYears); |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### Apply RQL |
| 98 | +```csharp |
| 99 | +public class UserQueryBuilder(IRqlQueryable<DbUser, User> rql) |
| 100 | +{ |
| 101 | + public IQueryable<User> GetUsersOlderThan(IQueryable<DbUser> sourceQuery, int age) |
| 102 | + { |
| 103 | + var request = new RqlRequest |
| 104 | + { |
| 105 | + Filter = $"gt(age,{age})", // Age must be greater than age specified |
| 106 | + Order = "-age", // Order by age desc |
| 107 | + Select = "id,name" // Select id and name |
| 108 | + }; |
| 109 | + |
| 110 | + return rql.Transform(sourceQuery, request).Query; |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
45 | 115 | ## Contributing |
46 | 116 |
|
47 | 117 | We welcome contributions to enhance the library. Please see our Contributing Guide for details: |
|
0 commit comments