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