How to use Specifications for Validation

A specification can be used to validate a given entity using the IsSatisfiedBy method. This method takes a single parameter of type T and returns a bool result.

public class AdultCustomerSpec : Specification<Customer>
{
    public AdultCustomerSpec()
    {
        Query.Where(x => x.Age >= 18);
    }
}
var customer = new Customer
{
    Age = 20
};

var spec = new AdultCustomerSpec();
bool isAdult = spec.IsSatisfiedBy(customer);

Note

The validation will utilize only the following functions, and ignore all other conditions defined in the specification.

  • Where
  • Search