< Summary

Information
Class: Pozitron.QuerySpecification.SpecificationEvaluator
Assembly: Pozitron.QuerySpecification.EntityFrameworkCore
File(s): /home/runner/work/QuerySpecification/QuerySpecification/src/QuerySpecification.EntityFrameworkCore/Evaluators/SpecificationEvaluator.cs
Tag: 67_15587897385
Line coverage
100%
Covered lines: 43
Uncovered lines: 0
Coverable lines: 43
Total lines: 111
Line coverage: 100%
Branch coverage
100%
Covered branches: 16
Total branches: 16
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%11100%
.ctor()50%22100%
.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>
 211    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>
 921    public SpecificationEvaluator()
 22    {
 923        Evaluators = TypeDiscovery.IsAutoDiscoveryEnabled
 924            ? TypeDiscovery.GetEvaluators()
 925            :
 926            [
 927                WhereEvaluator.Instance,
 928                LikeEvaluator.Instance,
 929                IncludeStringEvaluator.Instance,
 930                IncludeEvaluator.Instance,
 931                OrderEvaluator.Instance,
 932                QueryTagEvaluator.Instance,
 933                IgnoreAutoIncludesEvaluator.Instance,
 934                IgnoreQueryFiltersEvaluator.Instance,
 935                AsSplitQueryEvaluator.Instance,
 936                AsNoTrackingEvaluator.Instance,
 937                AsNoTrackingWithIdentityResolutionEvaluator.Instance,
 938                AsTrackingEvaluator.Instance,
 939            ];
 940    }
 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>
 146    public SpecificationEvaluator(IEnumerable<IEvaluator> evaluators)
 47    {
 148        Evaluators = evaluators.ToList();
 149    }
 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    {
 2365        ArgumentNullException.ThrowIfNull(specification);
 66
 2267        var selector = specification.Selector;
 2268        var selectorMany = specification.SelectorMany;
 69
 2270        if (selector is null && selectorMany is null)
 71        {
 172            throw new SelectorNotFoundException();
 73        }
 74
 2175        source = Evaluate(source, (Specification<T>)specification, true);
 76
 2177        var resultQuery = selector is not null
 2178            ? source.Select(selector)
 2179            : source.SelectMany(selectorMany!);
 80
 2181        return ignorePaging
 2182            ? resultQuery
 2183            : 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    {
 5299        ArgumentNullException.ThrowIfNull(specification);
 52100        if (specification.IsEmpty) return source;
 101
 1296102        foreach (var evaluator in Evaluators)
 103        {
 598104            source = evaluator.Evaluate(source, specification);
 105        }
 106
 50107        return ignorePaging
 50108            ? source
 50109            : source.ApplyPaging(specification);
 110    }
 111}