Empty

Feature Is supported
Null safe
Validation
EF Core
EF 6

Validation result

Error

"Object is not empty".

"Object is empty" - for negation.

Info

Checks if candidate is empty.

Empty means:

  • Null reference.
  • Empty string.
  • Collection without elements.
  • Object is equal to default value of T (default(T)).

Usage

var stringSpec = Specification.Empty<string>();
var arraySpec = Specification.Empty<int[]>();
var intSpec = Specification.Empty<int>();

stringSpec.IsSatisfiedBy(null); // true
stringSpec.IsSatisfiedBy("");   // true
stringSpec.IsSatisfiedBy("value");  // false

arraySpec.IsSatisfiedBy(null); // true
arraySpec.IsSatisfiedBy(new int[0]);    // true
arraySpec.IsSatisfiedBy(new int[] {10});    // false

intSpec.IsSatisfiedBy(0); // true
intSpec.IsSatisfiedBy(25); // false

As property

var customerSpec = Specification.Empty<Customer, ICollection<Item>>(c => c.Items);

customerSpec.IsSatisfiedBy(new Customer { Items = null }); // true
customerSpec.IsSatisfiedBy(new Customer { Items = new List<Item>() }); // true
customerSpec.IsSatisfiedBy(new Customer
{
    Items = new List<Item>
    {
        new Item()
    }
}); // false

Not Empty

var stringSpec = Specification.NotEmpty<string>();
var arraySpec = Specification.NotEmpty<int[]>();
var intSpec = Specification.NotEmpty<int>();

stringSpec.IsSatisfiedBy(null); // false
stringSpec.IsSatisfiedBy("");   // false
stringSpec.IsSatisfiedBy("value");  // true

arraySpec.IsSatisfiedBy(null); // false
arraySpec.IsSatisfiedBy(new int[0]);    // false
arraySpec.IsSatisfiedBy(new int[] { 10 });    // true

intSpec.IsSatisfiedBy(0); // false
intSpec.IsSatisfiedBy(25); // true

As property

var customerSpec = Specification.NotEmpty<Customer, ICollection<Item>>(c => c.Items);

customerSpec.IsSatisfiedBy(new Customer { Items = null }); // false
customerSpec.IsSatisfiedBy(new Customer { Items = new List<Item>() }); // false
customerSpec.IsSatisfiedBy(new Customer
{
    Items = new List<Item>
    {
        new Item()
    }
}); // 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 emptyItemsSpec = Specification.Empty<Customer, ICollection<Item>>(c => c.Items);
    // var customers = context.Customers.Where(emptyItemsSpec.GetExpression()).ToList();   // Exception!

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