Feature | Is supported |
---|---|
Null safe | |
Validation | |
EF Core | |
EF 6 |
Validation result
Error
"One or more elements are not specified".
Also Specification adds prefix with element index, for each error returned from element Specification.
Parameters
Parameter | Description |
---|---|
SpecificationForAll | Specification object passed in constructor. |
Info
Checks if ISpecification<T>
is satisfied by ALL elements in candidate collection.
All for empty collections are always valid (return true).
Candidate must implement IEnumerable<>
interface.
Note that, string
is also a collection, and can be used with this Specification.
Usage
var spec = Specification.All<int[], int>(
Specification.GreaterThanOrEqual(100));
spec.IsSatisfiedBy(new int[] {100, 200, 300}); // true
spec.IsSatisfiedBy(new int[] {1, 200, 300}); // false
spec.IsSatisfiedBy(new int[0]); // true!
spec.IsSatisfiedBy(null); // false
As property
var customerSpec = Specification.All<Customer, char>(c => c.Comments,
Specification.NotEqual('0'));
customerSpec.IsSatisfiedBy(new Customer { Comments = "VIP" }); // true
customerSpec.IsSatisfiedBy(new Customer { Comments = "0" }); // false
customerSpec.IsSatisfiedBy(new Customer { Comments = "" }); // true!
customerSpec.IsSatisfiedBy(new Customer { Comments = null }); // false
EF 6 support
In Linq to entities compare collections with null generates NotSupportedException
.
To prevent this, use linqToEntities
constructor flag or global Specification.LinqToEntities
flag.
using (var context = new EfDbContext())
{
var allItemsSpec = Specification.All<Customer, Item>(c => c.Items,
Specification.NotEmpty<Item, int>(i => i.ItemId));
// var customers = context.Customers.Where(allItemsSpec.GetExpression()).ToList(); // Exception!
Specification.LinqToEntities = true;
var allItemsSpecFixed = Specification.All<Customer, Item>(c => c.Items,
Specification.NotEmpty<Item, int>(i => i.ItemId));
var customers = context.Customers.Where(allItemsSpecFixed.GetExpression()).ToList(); // Works!
}