| | 1 | | namespace Pozitron.QuerySpecification; |
| | 2 | |
|
| | 3 | | public class Specification<T, TResult> : Specification<T> |
| | 4 | | { |
| | 5 | | public Specification() |
| | 6 | | { |
| | 7 | | Query = new SpecificationBuilder<T, TResult>(this); |
| | 8 | | } |
| | 9 | |
|
| | 10 | | public new ISpecificationBuilder<T, TResult> Query { get; } |
| | 11 | |
|
| | 12 | | public Expression<Func<T, TResult>>? Selector { get; internal set; } |
| | 13 | | public Expression<Func<T, IEnumerable<TResult>>>? SelectorMany { get; internal set; } |
| | 14 | |
|
| | 15 | | public new virtual IEnumerable<TResult> Evaluate(IEnumerable<T> entities, bool ignorePaging = false) |
| | 16 | | { |
| | 17 | | var evaluator = SpecificationInMemoryEvaluator.Default; |
| | 18 | | return evaluator.Evaluate(entities, this, ignorePaging); |
| | 19 | | } |
| | 20 | | } |
| | 21 | |
|
| | 22 | | public class Specification<T> |
| | 23 | | { |
| | 24 | | // The state is null initially, but we're spending 8 bytes per reference on x64. |
| | 25 | | // I considered keeping the whole state as a Dictionary<int, object>, and that reduces the footprint for empty specs |
| | 26 | | // But, specs are never empty, and the overhead of the dictionary compensates the saved space. Also casting back and |
| | 27 | | // Refer to SpecificationSizeTests for more details. |
| | 28 | | private List<WhereExpression<T>>? _whereExpressions; |
| | 29 | | private List<LikeExpression<T>>? _likeExpressions; |
| | 30 | | private List<OrderExpression<T>>? _orderExpressions; |
| | 31 | | private List<IncludeExpression>? _includeExpressions; |
| | 32 | | private List<string>? _includeStrings; |
| | 33 | | private Dictionary<string, object>? _items; |
| | 34 | |
|
| 242 | 35 | | public Specification() |
| | 36 | | { |
| 242 | 37 | | Query = new SpecificationBuilder<T>(this); |
| 242 | 38 | | } |
| | 39 | |
|
| 124 | 40 | | public ISpecificationBuilder<T> Query { get; } |
| | 41 | |
|
| 338 | 42 | | public int Take { get; internal set; } = -1; |
| 339 | 43 | | public int Skip { get; internal set; } = -1; |
| | 44 | |
|
| | 45 | | // Based on the alignment of 8 bytes (on x64) we can store 8 flags here |
| | 46 | | // So, we have space for 4 more flags for free. |
| 69 | 47 | | public bool IgnoreQueryFilters { get; internal set; } = false; |
| 61 | 48 | | public bool AsSplitQuery { get; internal set; } = false; |
| 74 | 49 | | public bool AsNoTracking { get; internal set; } = false; |
| 74 | 50 | | public bool AsNoTrackingWithIdentityResolution { get; internal set; } = false; |
| | 51 | |
|
| 77 | 52 | | internal void Add(WhereExpression<T> whereExpression) => (_whereExpressions ??= new(2)).Add(whereExpression); |
| 67 | 53 | | internal void Add(LikeExpression<T> likeExpression) => (_likeExpressions ??= new(2)).Add(likeExpression); |
| 92 | 54 | | internal void Add(OrderExpression<T> orderExpression) => (_orderExpressions ??= new(2)).Add(orderExpression); |
| 109 | 55 | | internal void Add(IncludeExpression includeExpression) => (_includeExpressions ??= new(2)).Add(includeExpression); |
| 15 | 56 | | internal void Add(string includeString) => (_includeStrings ??= new(1)).Add(includeString); |
| | 57 | |
|
| | 58 | |
|
| | 59 | | // Specs are not intended to be thread-safe, so we don't need to worry about thread-safety here. |
| 1 | 60 | | public Dictionary<string, object> Items => _items ??= []; |
| 86 | 61 | | public IEnumerable<WhereExpression<T>> WhereExpressions => _whereExpressions ?? Enumerable.Empty<WhereExpression<T>> |
| 117 | 62 | | public IEnumerable<LikeExpression<T>> LikeExpressions => _likeExpressions ?? Enumerable.Empty<LikeExpression<T>>(); |
| 168 | 63 | | public IEnumerable<OrderExpression<T>> OrderExpressions => _orderExpressions ?? Enumerable.Empty<OrderExpression<T>> |
| 90 | 64 | | public IEnumerable<IncludeExpression> IncludeExpressions => _includeExpressions ?? Enumerable.Empty<IncludeExpressio |
| 62 | 65 | | public IEnumerable<string> IncludeStrings => _includeStrings ?? Enumerable.Empty<string>(); |
| | 66 | |
|
| | 67 | | public virtual IEnumerable<T> Evaluate(IEnumerable<T> entities, bool ignorePaging = false) |
| | 68 | | { |
| 2 | 69 | | var evaluator = SpecificationInMemoryEvaluator.Default; |
| 2 | 70 | | return evaluator.Evaluate(entities, this, ignorePaging); |
| | 71 | | } |
| | 72 | |
|
| | 73 | | public virtual bool IsSatisfiedBy(T entity) |
| | 74 | | { |
| 3 | 75 | | var validator = SpecificationValidator.Default; |
| 3 | 76 | | return validator.IsValid(entity, this); |
| | 77 | | } |
| | 78 | | } |