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