Length Between

Feature Is supported
Null safe
Validation
EF Core
EF 6

Validation result

Error

"Object length is not between [{MinLength}] and [{MaxLength}]".

"Object length is between [{MinLength}] and [{MaxLength}]" - for negation.

Parameters

Parameter Description
MinLengthMin value passed in constructor.
MaxLengthMax value passed in constructor.

Info

Checks if length of candidate is between Min and Max values.

Candidate must implement IEnumerable interface.
Note that, string is also a collection, and can be used with this Specification.

Usage

var spec = Specification.LengthBetween<int[]>(3, 5);

spec.IsSatisfiedBy(new int[] {100, 200, 0});  // true
spec.IsSatisfiedBy(new int[] {100, 200});    // false
spec.IsSatisfiedBy(new int[0]);  // false
spec.IsSatisfiedBy(null);   // false

As property

var customerSpec = Specification.LengthBetween<Customer, string>(c => c.Comments, 3, 5);

customerSpec.IsSatisfiedBy(new Customer { Comments = "VIP" });  // true
customerSpec.IsSatisfiedBy(new Customer { Comments = "0" });    // false
customerSpec.IsSatisfiedBy(new Customer { Comments = "" });  // false
customerSpec.IsSatisfiedBy(new Customer { Comments = null });   // false

Not Length Between

var spec = Specification.NotLengthBetween<int[]>(3, 5);

spec.IsSatisfiedBy(new int[] {100, 200, 0});  // false
spec.IsSatisfiedBy(new int[] {100, 200});    // true
spec.IsSatisfiedBy(new int[0]);  // true
spec.IsSatisfiedBy(null);   // true

As property

var customerSpec = Specification.NotLengthBetween<Customer, string>(c => c.Comments, 3, 5);

customerSpec.IsSatisfiedBy(new Customer { Comments = "VIP" });  // false
customerSpec.IsSatisfiedBy(new Customer { Comments = "0" });    // true
customerSpec.IsSatisfiedBy(new Customer { Comments = "" });  // true
customerSpec.IsSatisfiedBy(new Customer { Comments = null });   // true

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 anyItemsSpec = Specification.LengthBetween<Customer, ICollection<Item>>(c => c.Items, 1, 5);
    // var customers = context.Customers.Where(anyItemsSpec.GetExpression()).ToList();   // Exception!

    Specification.LinqToEntities = true;
    var anyItemsSpecFixed = Specification.LengthBetween<Customer, ICollection<Item>>(c => c.Items, 1, 5);
    var customers = context.Customers.Where(anyItemsSpecFixed.GetExpression()).ToList();   // Works!
}
GitHub