| | 1 | | namespace Pozitron.QuerySpecification; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Evaluates specifications in memory. |
| | 5 | | /// </summary> |
| | 6 | | public class SpecificationMemoryEvaluator |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Gets the default instance of the <see cref="SpecificationMemoryEvaluator"/> class. |
| | 10 | | /// </summary> |
| 2 | 11 | | public static SpecificationMemoryEvaluator Default = new(); |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Gets the list of in-memory evaluators. |
| | 15 | | /// </summary> |
| 14 | 16 | | protected List<IMemoryEvaluator> Evaluators { get; } |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Initializes a new instance of the <see cref="SpecificationMemoryEvaluator"/> class. |
| | 20 | | /// </summary> |
| 5 | 21 | | public SpecificationMemoryEvaluator() |
| | 22 | | { |
| 5 | 23 | | Evaluators = TypeDiscovery.IsAutoDiscoveryEnabled |
| 5 | 24 | | ? TypeDiscovery.GetMemoryEvaluators() |
| 5 | 25 | | : |
| 5 | 26 | | [ |
| 5 | 27 | | WhereEvaluator.Instance, |
| 5 | 28 | | LikeMemoryEvaluator.Instance, |
| 5 | 29 | | OrderEvaluator.Instance, |
| 5 | 30 | | ]; |
| 5 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Initializes a new instance of the <see cref="SpecificationMemoryEvaluator"/> class with the specified evaluators |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="evaluators">The in-memory evaluators to use.</param> |
| 1 | 37 | | public SpecificationMemoryEvaluator(IEnumerable<IMemoryEvaluator> evaluators) |
| | 38 | | { |
| 1 | 39 | | Evaluators = evaluators.ToList(); |
| 1 | 40 | | } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Evaluates the given specification on the provided enumerable source and returns the result. |
| | 44 | | /// </summary> |
| | 45 | | /// <typeparam name="T">The type of the entity.</typeparam> |
| | 46 | | /// <typeparam name="TResult">The type of the result.</typeparam> |
| | 47 | | /// <param name="source">The enumerable source.</param> |
| | 48 | | /// <param name="specification">The specification to evaluate.</param> |
| | 49 | | /// <param name="ignorePaging">Whether to ignore paging settings (Take/Skip) defined in the specification.</param> |
| | 50 | | /// <returns>The evaluated enumerable result.</returns> |
| | 51 | | public virtual IEnumerable<TResult> Evaluate<T, TResult>( |
| | 52 | | IEnumerable<T> source, |
| | 53 | | Specification<T, TResult> specification, |
| | 54 | | bool ignorePaging = false) |
| | 55 | | { |
| 10 | 56 | | ArgumentNullException.ThrowIfNull(specification); |
| | 57 | |
|
| 9 | 58 | | var selector = specification.Selector; |
| 9 | 59 | | var selectorMany = specification.SelectorMany; |
| | 60 | |
|
| 9 | 61 | | if (selector is null && selectorMany is null) |
| | 62 | | { |
| 1 | 63 | | throw new SelectorNotFoundException(); |
| | 64 | | } |
| | 65 | |
|
| 8 | 66 | | source = Evaluate(source, (Specification<T>)specification, true); |
| | 67 | |
|
| 8 | 68 | | var result = selector is not null |
| 8 | 69 | | ? source.Select(selector.Compile()) |
| 8 | 70 | | : source.SelectMany(selectorMany!.Compile()); |
| | 71 | |
|
| 8 | 72 | | return ignorePaging |
| 8 | 73 | | ? result |
| 8 | 74 | | : result.ApplyPaging(specification); |
| | 75 | | } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Evaluates the given specification on the provided enumerable source and returns the result. |
| | 79 | | /// </summary> |
| | 80 | | /// <typeparam name="T">The type of the entity.</typeparam> |
| | 81 | | /// <param name="source">The enumerable source.</param> |
| | 82 | | /// <param name="specification">The specification to evaluate.</param> |
| | 83 | | /// <param name="ignorePaging">Whether to ignore paging settings (Take/Skip) defined in the specification.</param> |
| | 84 | | /// <returns>The evaluated enumerable result.</returns> |
| | 85 | | public virtual IEnumerable<T> Evaluate<T>( |
| | 86 | | IEnumerable<T> source, |
| | 87 | | Specification<T> specification, |
| | 88 | | bool ignorePaging = false) |
| | 89 | | { |
| 15 | 90 | | ArgumentNullException.ThrowIfNull(specification); |
| 16 | 91 | | if (specification.IsEmpty) return source; |
| | 92 | |
|
| 96 | 93 | | foreach (var evaluator in Evaluators) |
| | 94 | | { |
| 36 | 95 | | source = evaluator.Evaluate(source, specification); |
| | 96 | | } |
| | 97 | |
|
| 12 | 98 | | return ignorePaging |
| 12 | 99 | | ? source |
| 12 | 100 | | : source.ApplyPaging(specification); |
| | 101 | | } |
| | 102 | | } |