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