| | 1 | | namespace Pozitron.QuerySpecification; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Provides extension methods for <see cref="IQueryable{T}"/> to apply specifications and pagination. |
| | 5 | | /// </summary> |
| | 6 | | public static class IQueryableExtensions |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Applies the given specification to the queryable source. |
| | 10 | | /// </summary> |
| | 11 | | /// <typeparam name="TSource">The type of the entity.</typeparam> |
| | 12 | | /// <param name="source">The queryable source.</param> |
| | 13 | | /// <param name="specification">The specification to apply.</param> |
| | 14 | | /// <param name="evaluator">The specification evaluator to use. If null, the default evaluator is used.</param> |
| | 15 | | /// <returns>The queryable source with the specification applied.</returns> |
| | 16 | | public static IQueryable<TSource> WithSpecification<TSource>( |
| | 17 | | this IQueryable<TSource> source, |
| | 18 | | Specification<TSource> specification, |
| | 19 | | SpecificationEvaluator? evaluator = null) |
| | 20 | | where TSource : class |
| | 21 | | { |
| 3 | 22 | | evaluator ??= SpecificationEvaluator.Default; |
| 3 | 23 | | return evaluator.Evaluate(source, specification); |
| | 24 | | } |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Applies the given specification to the queryable source and projects the result to a different type. |
| | 28 | | /// </summary> |
| | 29 | | /// <typeparam name="TSource">The type of the entity.</typeparam> |
| | 30 | | /// <typeparam name="TResult">The type of the result.</typeparam> |
| | 31 | | /// <param name="source">The queryable source.</param> |
| | 32 | | /// <param name="specification">The specification to apply.</param> |
| | 33 | | /// <param name="evaluator">The specification evaluator to use. If null, the default evaluator is used.</param> |
| | 34 | | /// <returns>The queryable source with the specification applied and projected to the result type.</returns> |
| | 35 | | public static IQueryable<TResult> WithSpecification<TSource, TResult>( |
| | 36 | | this IQueryable<TSource> source, |
| | 37 | | Specification<TSource, TResult> specification, |
| | 38 | | SpecificationEvaluator? evaluator = null) |
| | 39 | | where TSource : class |
| | 40 | | { |
| 5 | 41 | | evaluator ??= SpecificationEvaluator.Default; |
| 5 | 42 | | return evaluator.Evaluate(source, specification); |
| | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Converts the queryable source to a paged result asynchronously. |
| | 47 | | /// </summary> |
| | 48 | | /// <typeparam name="TSource">The type of the entity.</typeparam> |
| | 49 | | /// <param name="source">The queryable source.</param> |
| | 50 | | /// <param name="filter">The paging filter.</param> |
| | 51 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 52 | | /// <returns>A task that represents the asynchronous operation. The task result contains the paged result.</returns> |
| | 53 | | public static Task<PagedResult<TSource>> ToPagedResultAsync<TSource>( |
| | 54 | | this IQueryable<TSource> source, |
| | 55 | | PagingFilter filter, |
| | 56 | | CancellationToken cancellationToken = default) |
| | 57 | | where TSource : class |
| 2 | 58 | | => ToPagedResultAsync(source, filter, PaginationSettings.Default, cancellationToken); |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Converts the queryable source to a paged result asynchronously with the specified pagination settings. |
| | 62 | | /// </summary> |
| | 63 | | /// <typeparam name="TSource">The type of the entity.</typeparam> |
| | 64 | | /// <param name="source">The queryable source.</param> |
| | 65 | | /// <param name="filter">The paging filter.</param> |
| | 66 | | /// <param name="paginationSettings">The pagination settings.</param> |
| | 67 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 68 | | /// <returns>A task that represents the asynchronous operation. The task result contains the paged result.</returns> |
| | 69 | | public static async Task<PagedResult<TSource>> ToPagedResultAsync<TSource>( |
| | 70 | | this IQueryable<TSource> source, |
| | 71 | | PagingFilter filter, |
| | 72 | | PaginationSettings paginationSettings, |
| | 73 | | CancellationToken cancellationToken = default) |
| | 74 | | where TSource : class |
| | 75 | | { |
| 3 | 76 | | var count = await source.CountAsync(cancellationToken); |
| 3 | 77 | | var pagination = new Pagination(paginationSettings, count, filter); |
| | 78 | |
|
| 3 | 79 | | source = source.ApplyPaging(pagination); |
| | 80 | |
|
| 3 | 81 | | var data = await source.ToListAsync(cancellationToken); |
| | 82 | |
|
| 3 | 83 | | return new PagedResult<TSource>(data, pagination); |
| 3 | 84 | | } |
| | 85 | | } |