-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from ardalis/v5
V5
- Loading branch information
Showing
78 changed files
with
1,005 additions
and
529 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
ArdalisSpecification/src/Ardalis.Specification/Evaluators/IEvaluator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Ardalis.Specification | ||
{ | ||
public interface IEvaluator | ||
{ | ||
bool IsCriteriaEvaluator { get; } | ||
|
||
IQueryable<T> GetQuery<T>(IQueryable<T> query, ISpecification<T> specification) where T : class; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
ArdalisSpecification/src/Ardalis.Specification/Evaluators/IInMemoryEvaluator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Ardalis.Specification | ||
{ | ||
public interface IInMemoryEvaluator | ||
{ | ||
IEnumerable<T> Evaluate<T>(IEnumerable<T> query, ISpecification<T> specification); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
ArdalisSpecification/src/Ardalis.Specification/Evaluators/IInMemorySpecificationEvaluator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Ardalis.Specification | ||
{ | ||
public interface IInMemorySpecificationEvaluator | ||
{ | ||
IEnumerable<TResult> Evaluate<T, TResult>(IEnumerable<T> source, ISpecification<T, TResult> specification); | ||
IEnumerable<T> Evaluate<T>(IEnumerable<T> source, ISpecification<T> specification); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
ArdalisSpecification/src/Ardalis.Specification/Evaluators/InMemorySpecificationEvaluator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Text; | ||
|
||
namespace Ardalis.Specification | ||
{ | ||
public class InMemorySpecificationEvaluator : IInMemorySpecificationEvaluator | ||
{ | ||
// Will use singleton for default configuration. Yet, it can be instantiated if necessary, with default or provided evaluators. | ||
public static InMemorySpecificationEvaluator Default { get; } = new InMemorySpecificationEvaluator(); | ||
|
||
private readonly List<IInMemoryEvaluator> evaluators = new List<IInMemoryEvaluator>(); | ||
|
||
public InMemorySpecificationEvaluator() | ||
{ | ||
this.evaluators.AddRange(new IInMemoryEvaluator[] | ||
{ | ||
WhereEvaluator.Instance, | ||
OrderEvaluator.Instance, | ||
PaginationEvaluator.Instance | ||
}); | ||
} | ||
public InMemorySpecificationEvaluator(IEnumerable<IInMemoryEvaluator> evaluators) | ||
{ | ||
this.evaluators.AddRange(evaluators); | ||
} | ||
|
||
public virtual IEnumerable<TResult> Evaluate<T, TResult>(IEnumerable<T> source, ISpecification<T, TResult> specification) | ||
{ | ||
_ = specification.Selector ?? throw new SelectorNotFoundException(); | ||
|
||
var baseQuery = Evaluate(source, (ISpecification<T>)specification); | ||
|
||
var resultQuery = baseQuery.Select(specification.Selector.Compile()); | ||
|
||
return specification.PostProcessingAction == null | ||
? resultQuery | ||
: specification.PostProcessingAction(resultQuery); | ||
} | ||
|
||
public virtual IEnumerable<T> Evaluate<T>(IEnumerable<T> source, ISpecification<T> specification) | ||
{ | ||
if (specification.SearchCriterias.Count() > 0) | ||
{ | ||
throw new NotSupportedException("The specification contains Search expressions and can't be evaluated with in-memory evaluator."); | ||
} | ||
|
||
foreach (var evaluator in evaluators) | ||
{ | ||
source = evaluator.Evaluate(source, specification); | ||
} | ||
|
||
return specification.PostProcessingAction == null | ||
? source | ||
: specification.PostProcessingAction(source); | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
ArdalisSpecification/src/Ardalis.Specification/Evaluators/OrderEvaluator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,95 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Ardalis.Specification | ||
{ | ||
public class OrderEvaluator : IEvaluator, IInMemoryEvaluator | ||
{ | ||
private OrderEvaluator() { } | ||
public static OrderEvaluator Instance { get; } = new OrderEvaluator(); | ||
|
||
public bool IsCriteriaEvaluator { get; } = false; | ||
|
||
public IQueryable<T> GetQuery<T>(IQueryable<T> query, ISpecification<T> specification) where T : class | ||
{ | ||
if (specification.OrderExpressions != null) | ||
{ | ||
if (specification.OrderExpressions.Where(x => x.OrderType == OrderTypeEnum.OrderBy || | ||
x.OrderType == OrderTypeEnum.OrderByDescending).Count() > 1) | ||
{ | ||
throw new DuplicateOrderChainException(); | ||
} | ||
|
||
IOrderedQueryable<T>? orderedQuery = null; | ||
foreach (var orderExpression in specification.OrderExpressions) | ||
{ | ||
if (orderExpression.OrderType == OrderTypeEnum.OrderBy) | ||
{ | ||
orderedQuery = query.OrderBy(orderExpression.KeySelector); | ||
} | ||
else if (orderExpression.OrderType == OrderTypeEnum.OrderByDescending) | ||
{ | ||
orderedQuery = query.OrderByDescending(orderExpression.KeySelector); | ||
} | ||
else if (orderExpression.OrderType == OrderTypeEnum.ThenBy) | ||
{ | ||
orderedQuery = orderedQuery.ThenBy(orderExpression.KeySelector); | ||
} | ||
else if (orderExpression.OrderType == OrderTypeEnum.ThenByDescending) | ||
{ | ||
orderedQuery = orderedQuery.ThenByDescending(orderExpression.KeySelector); | ||
} | ||
} | ||
|
||
if (orderedQuery != null) | ||
{ | ||
query = orderedQuery; | ||
} | ||
} | ||
|
||
return query; | ||
} | ||
|
||
public IEnumerable<T> Evaluate<T>(IEnumerable<T> query, ISpecification<T> specification) | ||
{ | ||
if (specification.OrderExpressions != null) | ||
{ | ||
if (specification.OrderExpressions.Where(x => x.OrderType == OrderTypeEnum.OrderBy || | ||
x.OrderType == OrderTypeEnum.OrderByDescending).Count() > 1) | ||
{ | ||
throw new DuplicateOrderChainException(); | ||
} | ||
|
||
IOrderedEnumerable<T>? orderedQuery = null; | ||
foreach (var orderExpression in specification.OrderExpressions) | ||
{ | ||
if (orderExpression.OrderType == OrderTypeEnum.OrderBy) | ||
{ | ||
orderedQuery = query.OrderBy(orderExpression.KeySelector.Compile()); | ||
} | ||
else if (orderExpression.OrderType == OrderTypeEnum.OrderByDescending) | ||
{ | ||
orderedQuery = query.OrderByDescending(orderExpression.KeySelector.Compile()); | ||
} | ||
else if (orderExpression.OrderType == OrderTypeEnum.ThenBy) | ||
{ | ||
orderedQuery = orderedQuery.ThenBy(orderExpression.KeySelector.Compile()); | ||
} | ||
else if (orderExpression.OrderType == OrderTypeEnum.ThenByDescending) | ||
{ | ||
orderedQuery = orderedQuery.ThenByDescending(orderExpression.KeySelector.Compile()); | ||
} | ||
} | ||
|
||
if (orderedQuery != null) | ||
{ | ||
query = orderedQuery; | ||
} | ||
} | ||
|
||
return query; | ||
} | ||
} | ||
} |
Oops, something went wrong.