| | 1 | | namespace Pozitron.QuerySpecification; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Represents an evaluator for order expressions. |
| | 5 | | /// </summary> |
| | 6 | | [EvaluatorDiscovery(Order = 50)] |
| | 7 | | public sealed class OrderEvaluator : IEvaluator, IMemoryEvaluator |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Gets the singleton instance of the <see cref="OrderEvaluator"/> class. |
| | 11 | | /// </summary> |
| 3 | 12 | | public static OrderEvaluator Instance = new(); |
| 6 | 13 | | private OrderEvaluator() { } |
| | 14 | |
|
| | 15 | | /// <inheritdoc/> |
| | 16 | | public IQueryable<T> Evaluate<T>(IQueryable<T> source, Specification<T> specification) where T : class |
| | 17 | | { |
| 60 | 18 | | IOrderedQueryable<T>? orderedQuery = null; |
| | 19 | |
|
| 660 | 20 | | foreach (var item in specification.Items) |
| | 21 | | { |
| 270 | 22 | | if (item.Type == ItemType.Order && item.Reference is Expression<Func<T, object?>> expr) |
| | 23 | | { |
| 40 | 24 | | if (item.Bag == (int)OrderType.OrderBy) |
| | 25 | | { |
| 22 | 26 | | orderedQuery = source.OrderBy(expr); |
| | 27 | | } |
| 18 | 28 | | else if (item.Bag == (int)OrderType.OrderByDescending) |
| | 29 | | { |
| 4 | 30 | | orderedQuery = source.OrderByDescending(expr); |
| | 31 | | } |
| 14 | 32 | | else if (item.Bag == (int)OrderType.ThenBy) |
| | 33 | | { |
| 4 | 34 | | orderedQuery = orderedQuery!.ThenBy(expr); |
| | 35 | | } |
| 10 | 36 | | else if (item.Bag == (int)OrderType.ThenByDescending) |
| | 37 | | { |
| 10 | 38 | | orderedQuery = orderedQuery!.ThenByDescending(expr); |
| | 39 | | } |
| | 40 | | } |
| | 41 | | } |
| | 42 | |
|
| 60 | 43 | | if (orderedQuery is not null) |
| | 44 | | { |
| 25 | 45 | | source = orderedQuery; |
| | 46 | | } |
| | 47 | |
|
| 60 | 48 | | return source; |
| | 49 | | } |
| | 50 | |
|
| | 51 | | /// <inheritdoc/> |
| | 52 | | public IEnumerable<T> Evaluate<T>(IEnumerable<T> source, Specification<T> specification) |
| | 53 | | { |
| 19 | 54 | | var compiledItems = specification.GetCompiledItems(); |
| 19 | 55 | | IOrderedEnumerable<T>? orderedQuery = null; |
| | 56 | |
|
| 106 | 57 | | foreach (var item in compiledItems) |
| | 58 | | { |
| 34 | 59 | | if (item.Type == ItemType.Order && item.Reference is Func<T, object?> compiledExpr) |
| | 60 | | { |
| 22 | 61 | | if (item.Bag == (int)OrderType.OrderBy) |
| | 62 | | { |
| 15 | 63 | | orderedQuery = source.OrderBy(compiledExpr); |
| | 64 | | } |
| 7 | 65 | | else if (item.Bag == (int)OrderType.OrderByDescending) |
| | 66 | | { |
| 3 | 67 | | orderedQuery = source.OrderByDescending(compiledExpr); |
| | 68 | | } |
| 4 | 69 | | else if (item.Bag == (int)OrderType.ThenBy) |
| | 70 | | { |
| 2 | 71 | | orderedQuery = orderedQuery!.ThenBy(compiledExpr); |
| | 72 | | } |
| 2 | 73 | | else if (item.Bag == (int)OrderType.ThenByDescending) |
| | 74 | | { |
| 2 | 75 | | orderedQuery = orderedQuery!.ThenByDescending(compiledExpr); |
| | 76 | | } |
| | 77 | | } |
| | 78 | | } |
| | 79 | |
|
| 19 | 80 | | if (orderedQuery is not null) |
| | 81 | | { |
| 18 | 82 | | source = orderedQuery; |
| | 83 | | } |
| | 84 | |
|
| 19 | 85 | | return source; |
| | 86 | | } |
| | 87 | | } |