-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathSpecification.cs
More file actions
212 lines (167 loc) · 8.21 KB
/
Specification.cs
File metadata and controls
212 lines (167 loc) · 8.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System.ComponentModel;
namespace Ardalis.Specification;
/// <inheritdoc cref="ISpecification{T, TResult}"/>
public class Specification<T, TResult> : Specification<T>, ISpecification<T, TResult>
{
public new ISpecificationBuilder<T, TResult> Query => new SpecificationBuilder<T, TResult>(this);
/// <inheritdoc/>
public Expression<Func<T, TResult>>? Selector { get; internal set; }
/// <inheritdoc/>
public Expression<Func<T, IEnumerable<TResult>>>? SelectorMany { get; internal set; }
/// <inheritdoc/>
public new Func<IEnumerable<TResult>, IEnumerable<TResult>>? PostProcessingAction { get; internal set; } = null;
public new virtual IEnumerable<TResult> Evaluate(IEnumerable<T> entities)
{
var evaluator = Evaluator;
return evaluator.Evaluate(entities, this);
}
}
/// <inheritdoc cref="ISpecification{T}"/>
public class Specification<T> : ISpecification<T>
{
private const int DEFAULT_CAPACITY_SEARCH = 2;
// It is utilized only during the building stage for the sub-chains. Once the state is built, we don't care about it anymore.
// The initial value is not important since the value is always initialized by the root of the chain.
// Therefore, we don't need ThreadLocal (it's more expensive).
// With this we're saving 8 bytes per include builder, and we don't need an order builder at all (saving 32 bytes per order builder instance).
[ThreadStatic]
[EditorBrowsable(EditorBrowsableState.Never)]
public static bool IsChainDiscarded;
// The state is null initially, but we're spending 8 bytes per reference (on x64).
// This will be reconsidered for version 10 where we may store the whole state as a single array of structs.
private OneOrMany<WhereExpressionInfo<T>> _whereExpressions = new();
private List<SearchExpressionInfo<T>>? _searchExpressions;
private OneOrMany<OrderExpressionInfo<T>> _orderExpressions = new();
private OneOrMany<IncludeExpressionInfo> _includeExpressions = new();
private OneOrMany<string> _includeStrings = new();
private Dictionary<string, object>? _items;
private OneOrMany<string> _queryTags = new();
public ISpecificationBuilder<T> Query => new SpecificationBuilder<T>(this);
protected virtual IInMemorySpecificationEvaluator Evaluator => InMemorySpecificationEvaluator.Default;
protected virtual ISpecificationValidator Validator => SpecificationValidator.Default;
/// <inheritdoc/>
public Func<IEnumerable<T>, IEnumerable<T>>? PostProcessingAction { get; internal set; }
/// <inheritdoc/>
public string? CacheKey { get; internal set; }
/// <inheritdoc/>
public bool CacheEnabled => CacheKey is not null;
/// <inheritdoc/>
public int Take { get; internal set; } = -1;
/// <inheritdoc/>
public int Skip { get; internal set; } = -1;
// We may store all the flags in a single byte. But, based on the object alignment of 8 bytes, we won't save any space anyway.
// And we'll have unnecessary overhead with enum flags for now. This will be reconsidered for version 10.
// Based on the alignment of 8 bytes (on x64) we can store 8 flags here. So, we have space for 2 more flags for free.
/// <inheritdoc/>
public bool IgnoreQueryFilters { get; internal set; } = false;
/// <inheritdoc/>
public bool IgnoreAutoIncludes { get; internal set; } = false;
/// <inheritdoc/>
public bool AsSplitQuery { get; internal set; } = false;
/// <inheritdoc/>
public bool AsNoTracking { get; internal set; } = false;
/// <inheritdoc/>
public bool AsTracking { get; internal set; } = false;
/// <inheritdoc/>
public bool AsNoTrackingWithIdentityResolution { get; internal set; } = false;
// Specs are not intended to be thread-safe, so we don't need to worry about thread-safety here.
internal void Add(WhereExpressionInfo<T> whereExpression) => _whereExpressions.Add(whereExpression);
internal void Add(OrderExpressionInfo<T> orderExpression) => _orderExpressions.Add(orderExpression);
internal void Add(IncludeExpressionInfo includeExpression) => _includeExpressions.Add(includeExpression);
internal void Add(string includeString) => _includeStrings.Add(includeString);
internal void Add(SearchExpressionInfo<T> searchExpression)
{
if (_searchExpressions is null)
{
_searchExpressions = new(DEFAULT_CAPACITY_SEARCH) { searchExpression };
return;
}
// We'll keep the search expressions sorted by the search group.
// We could keep the state as SortedList instead of List, but it has additional 56 bytes overhead and it's not worth it for our use-case.
// Having multiple search groups is not a common scenario, and usually there may be just few search expressions.
var index = _searchExpressions.FindIndex(x => x.SearchGroup > searchExpression.SearchGroup);
if (index == -1)
{
_searchExpressions.Add(searchExpression);
}
else
{
_searchExpressions.Insert(index, searchExpression);
}
}
internal void AddQueryTag(string queryTag) => _queryTags.Add(queryTag);
/// <inheritdoc/>
public Dictionary<string, object> Items => _items ??= [];
/// <inheritdoc/>
public IEnumerable<WhereExpressionInfo<T>> WhereExpressions => _whereExpressions.Values;
/// <inheritdoc/>
public IEnumerable<SearchExpressionInfo<T>> SearchCriterias => _searchExpressions ?? Enumerable.Empty<SearchExpressionInfo<T>>();
/// <inheritdoc/>
public IEnumerable<OrderExpressionInfo<T>> OrderExpressions => _orderExpressions.Values;
/// <inheritdoc/>
public IEnumerable<IncludeExpressionInfo> IncludeExpressions => _includeExpressions.Values;
/// <inheritdoc/>
public IEnumerable<string> IncludeStrings => _includeStrings.Values;
/// <inheritdoc/>
public IEnumerable<string> QueryTags => _queryTags.Values;
internal OneOrMany<WhereExpressionInfo<T>> OneOrManyWhereExpressions => _whereExpressions;
internal OneOrMany<OrderExpressionInfo<T>> OneOrManyOrderExpressions => _orderExpressions;
internal OneOrMany<IncludeExpressionInfo> OneOrManyIncludeExpressions => _includeExpressions;
internal OneOrMany<string> OneOrManyIncludeStrings => _includeStrings;
internal OneOrMany<string> OneOrManyQueryTags => _queryTags;
/// <inheritdoc/>
public virtual IEnumerable<T> Evaluate(IEnumerable<T> entities)
{
var evaluator = Evaluator;
return evaluator.Evaluate(entities, this);
}
/// <inheritdoc/>
public virtual bool IsSatisfiedBy(T entity)
{
var validator = Validator;
return validator.IsValid(entity, this);
}
void ISpecification<T>.CopyTo(Specification<T> otherSpec)
{
otherSpec.PostProcessingAction = PostProcessingAction;
otherSpec.CacheKey = CacheKey;
otherSpec.Take = Take;
otherSpec.Skip = Skip;
otherSpec.IgnoreQueryFilters = IgnoreQueryFilters;
otherSpec.IgnoreAutoIncludes = IgnoreAutoIncludes;
otherSpec.AsSplitQuery = AsSplitQuery;
otherSpec.AsNoTracking = AsNoTracking;
otherSpec.AsTracking = AsTracking;
otherSpec.AsNoTrackingWithIdentityResolution = AsNoTrackingWithIdentityResolution;
// The expression containers are immutable, having the same instance is fine.
// We'll just create new collections.
if (!_whereExpressions.IsEmpty)
{
otherSpec._whereExpressions = _whereExpressions.Clone();
}
if (!_includeExpressions.IsEmpty)
{
otherSpec._includeExpressions = _includeExpressions.Clone();
}
if (!_includeStrings.IsEmpty)
{
otherSpec._includeStrings = _includeStrings.Clone();
}
if (!_orderExpressions.IsEmpty)
{
otherSpec._orderExpressions = _orderExpressions.Clone();
}
if (_searchExpressions is not null)
{
otherSpec._searchExpressions = _searchExpressions.ToList();
}
if (!_queryTags.IsEmpty)
{
otherSpec._queryTags = _queryTags.Clone();
}
if (_items is not null)
{
otherSpec._items = new Dictionary<string, object>(_items);
}
}
}