< Summary

Information
Class: Pozitron.QuerySpecification.SpecificationEvaluator
Assembly: Pozitron.QuerySpecification.EntityFrameworkCore
File(s): /home/runner/work/QuerySpecification/QuerySpecification/src/QuerySpecification.EntityFrameworkCore/Evaluators/SpecificationEvaluator.cs
Tag: 52_11740816328
Line coverage
100%
Covered lines: 39
Uncovered lines: 0
Coverable lines: 39
Total lines: 107
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
get_Evaluators()100%11100%
.ctor()100%11100%
.ctor(...)100%11100%
Evaluate(...)100%88100%
Evaluate(...)100%66100%

File(s)

/home/runner/work/QuerySpecification/QuerySpecification/src/QuerySpecification.EntityFrameworkCore/Evaluators/SpecificationEvaluator.cs

#LineLine coverage
 1namespace Pozitron.QuerySpecification;
 2
 3/// <summary>
 4/// Evaluates specifications by applying a series of evaluators.
 5/// </summary>
 6public class SpecificationEvaluator
 7{
 8    /// <summary>
 9    /// Gets the default instance of the <see cref="SpecificationEvaluator"/> class.
 10    /// </summary>
 111    public static SpecificationEvaluator Default = new();
 12
 13    /// <summary>
 14    /// Gets the list of evaluators.
 15    /// </summary>
 5416    protected List<IEvaluator> Evaluators { get; }
 17
 18    /// <summary>
 19    /// Initializes a new instance of the <see cref="SpecificationEvaluator"/> class.
 20    /// </summary>
 621    public SpecificationEvaluator()
 22    {
 623        Evaluators =
 624        [
 625            WhereEvaluator.Instance,
 626            LikeEvaluator.Instance,
 627            IncludeStringEvaluator.Instance,
 628            IncludeEvaluator.Instance,
 629            OrderEvaluator.Instance,
 630            IgnoreQueryFiltersEvaluator.Instance,
 631            AsNoTrackingEvaluator.Instance,
 632            AsNoTrackingWithIdentityResolutionEvaluator.Instance,
 633            AsTrackingEvaluator.Instance,
 634            AsSplitQueryEvaluator.Instance,
 635        ];
 636    }
 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>
 142    public SpecificationEvaluator(IEnumerable<IEvaluator> evaluators)
 43    {
 144        Evaluators = evaluators.ToList();
 145    }
 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    {
 2361        ArgumentNullException.ThrowIfNull(specification);
 62
 2263        var selector = specification.Selector;
 2264        var selectorMany = specification.SelectorMany;
 65
 2266        if (selector is null && selectorMany is null)
 67        {
 168            throw new SelectorNotFoundException();
 69        }
 70
 2171        source = Evaluate(source, (Specification<T>)specification, true);
 72
 2173        var resultQuery = selector is not null
 2174            ? source.Select(selector)
 2175            : source.SelectMany(selectorMany!);
 76
 2177        return ignorePaging
 2178            ? resultQuery
 2179            : 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    {
 5295        ArgumentNullException.ThrowIfNull(specification);
 5296        if (specification.IsEmpty) return source;
 97
 109698        foreach (var evaluator in Evaluators)
 99        {
 498100            source = evaluator.Evaluate(source, specification);
 101        }
 102
 50103        return ignorePaging
 50104            ? source
 50105            : source.ApplyPaging(specification);
 106    }
 107}