Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/Ardalis.Specification/Evaluators/WhereEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,37 @@ private WhereEvaluator() { }

public IQueryable<T> GetQuery<T>(IQueryable<T> query, ISpecification<T> specification) where T : class
{
foreach (var info in specification.WhereExpressions)
if (specification is Specification<T> spec)
{
query = query.Where(info.Filter);
if (spec.OneOrManyWhereExpressions.IsEmpty) return query;
if (spec.OneOrManyWhereExpressions.SingleOrDefault is { } whereExpression)
{
return query.Where(whereExpression.Filter);
}
}

foreach (var whereExpression in specification.WhereExpressions)
{
query = query.Where(whereExpression.Filter);
}

return query;
}

public IEnumerable<T> Evaluate<T>(IEnumerable<T> query, ISpecification<T> specification)
{
foreach (var info in specification.WhereExpressions)
if (specification is Specification<T> spec)
{
if (spec.OneOrManyWhereExpressions.IsEmpty) return query;
if (spec.OneOrManyWhereExpressions.SingleOrDefault is { } whereExpression)
{
return query.Where(whereExpression.FilterFunc);
}
}

foreach (var whereExpression in specification.WhereExpressions)
{
query = query.Where(info.FilterFunc);
query = query.Where(whereExpression.FilterFunc);
}

return query;
Expand Down
5 changes: 3 additions & 2 deletions src/Ardalis.Specification/Internals/OneOrMany.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

internal struct OneOrMany<T> where T : class
{
private const int DEFAULT_CAPACITY = 2;
private object? _value;

public readonly bool IsEmpty => _value is null;
Expand Down Expand Up @@ -58,11 +59,11 @@ public void AddSorted(T item, IComparer<T> comparer)
{
if (comparer.Compare(item, singleValue) <= 0)
{
_value = new List<T>(2) { item, singleValue };
_value = new List<T>(DEFAULT_CAPACITY) { item, singleValue };
}
else
{
_value = new List<T>(2) { singleValue, item };
_value = new List<T>(DEFAULT_CAPACITY) { singleValue, item };
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/Ardalis.Specification/Specification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class Specification<T, TResult> : Specification<T>, ISpecification<T, TRe
/// <inheritdoc cref="ISpecification{T}"/>
public class Specification<T> : ISpecification<T>
{
private const int DEFAULT_CAPACITY_WHERE = 2;
private const int DEFAULT_CAPACITY_SEARCH = 2;
private const int DEFAULT_CAPACITY_ORDER = 2;
private const int DEFAULT_CAPACITY_INCLUDE = 2;
Expand All @@ -42,7 +41,7 @@ public class Specification<T> : ISpecification<T>

// 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 List<WhereExpressionInfo<T>>? _whereExpressions;
private OneOrMany<WhereExpressionInfo<T>> _whereExpressions = new();
private List<SearchExpressionInfo<T>>? _searchExpressions;
private List<OrderExpressionInfo<T>>? _orderExpressions;
private List<IncludeExpressionInfo>? _includeExpressions;
Expand Down Expand Up @@ -94,7 +93,7 @@ public class Specification<T> : ISpecification<T>


// 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 ??= new(DEFAULT_CAPACITY_WHERE)).Add(whereExpression);
internal void Add(WhereExpressionInfo<T> whereExpression) => _whereExpressions.Add(whereExpression);
internal void Add(OrderExpressionInfo<T> orderExpression) => (_orderExpressions ??= new(DEFAULT_CAPACITY_ORDER)).Add(orderExpression);
internal void Add(IncludeExpressionInfo includeExpression) => (_includeExpressions ??= new(DEFAULT_CAPACITY_INCLUDE)).Add(includeExpression);
internal void Add(string includeString) => (_includeStrings ??= new(DEFAULT_CAPACITY_INCLUDESTRING)).Add(includeString);
Expand Down Expand Up @@ -125,7 +124,7 @@ internal void Add(SearchExpressionInfo<T> searchExpression)
public Dictionary<string, object> Items => _items ??= [];

/// <inheritdoc/>
public IEnumerable<WhereExpressionInfo<T>> WhereExpressions => _whereExpressions ?? Enumerable.Empty<WhereExpressionInfo<T>>();
public IEnumerable<WhereExpressionInfo<T>> WhereExpressions => _whereExpressions.Values;

/// <inheritdoc/>
public IEnumerable<SearchExpressionInfo<T>> SearchCriterias => _searchExpressions ?? Enumerable.Empty<SearchExpressionInfo<T>>();
Expand All @@ -142,6 +141,7 @@ internal void Add(SearchExpressionInfo<T> searchExpression)
/// <inheritdoc/>
public IEnumerable<string> QueryTags => _queryTags.Values;

internal OneOrMany<WhereExpressionInfo<T>> OneOrManyWhereExpressions => _whereExpressions;
internal OneOrMany<string> OneOrManyQueryTags => _queryTags;

/// <inheritdoc/>
Expand Down Expand Up @@ -174,9 +174,9 @@ void ISpecification<T>.CopyTo(Specification<T> otherSpec)
// The expression containers are immutable, having the same instance is fine.
// We'll just create new collections.

if (_whereExpressions is not null)
if (!_whereExpressions.IsEmpty)
{
otherSpec._whereExpressions = _whereExpressions.ToList();
otherSpec._whereExpressions = _whereExpressions.Clone();
}

if (_includeExpressions is not null)
Expand Down
13 changes: 11 additions & 2 deletions src/Ardalis.Specification/Validators/WhereValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@ private WhereValidator() { }

public bool IsValid<T>(T entity, ISpecification<T> specification)
{
foreach (var info in specification.WhereExpressions)
if (specification is Specification<T> spec)
{
if (info.FilterFunc(entity) == false) return false;
if (spec.OneOrManyWhereExpressions.IsEmpty) return true;
if (spec.OneOrManyWhereExpressions.SingleOrDefault is { } whereExpression)
{
return whereExpression.FilterFunc(entity);
}
}

foreach (var whereExpression in specification.WhereExpressions)
{
if (whereExpression.FilterFunc(entity) == false) return false;
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class WhereEvaluatorTests
public record Customer(int Id);

[Fact]
public void Filters_GivenWhereExpression()
public void Filters_GivenSingleWhereExpression()
{
List<Customer> input = [new(1), new(2), new(3), new(4), new(5)];
List<Customer> expected = [new(4), new(5)];
Expand All @@ -19,6 +19,20 @@ public void Filters_GivenWhereExpression()
Assert(spec, input, expected);
}

[Fact]
public void Filters_GivenMultipleWhereExpressions()
{
List<Customer> input = [new(1), new(2), new(3), new(4), new(5)];
List<Customer> expected = [new(4)];

var spec = new Specification<Customer>();
spec.Query
.Where(x => x.Id > 3)
.Where(x => x.Id < 5);

Assert(spec, input, expected);
}

[Fact]
public void DoesNotFilter_GivenNoWhereExpression()
{
Expand Down