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