For Property

Feature Is supported
Null safe
Validation
EF Core
EF 6

Validation result

Error

"Field '{PropertyName}' value is not valid".

Also Specification adds prefix "Field '{PropertyName}':" for each error returned from property value Specification.

Parameters

Parameter Description
PropertySelectorSelector passed in constructor.
PropertyNameName of candidate property.
PropertySpecificationSpecification object passed in constructor.

Info

Verifies if Specification is satisfied by candidate property value.

Usage

var spec = Specification
    .ForProperty<Customer, CreditCard>(c => c.CreditCard, 
        Specification.NotNull<CreditCard>());

spec.IsSatisfiedBy(
    new Customer {CreditCard = new CreditCard()});  // true
spec.IsSatisfiedBy(
    new Customer {CreditCard = null});  // false


// With sub property
var emailSpec = Specification
    .ForProperty<Customer, string>(c => c.Caretaker.Email,
        Specification.Email());

spec.IsSatisfiedBy(
    new Customer
    {
        Caretaker = new Customer
        {
            Email = "john.doe@email.com"
        }
    });  // true

Extensions

Every built-in Specification has extension with PropertySpecification<T, TProperty>.

Specification
    .ForProperty<Customer, int>(c => c.CustomerId,
        Specification.GreaterThan(0));

// ... is equivalent with:
Specification.GreaterThan<Customer, int>(
    c => c.CustomerId, 0);

Null support

PropertySpecification<T, TProperty> doesn't throw NullReferenceException, when candidate or his sub-property is null, but returns false.
Null as value of property works normally.

var spec = Specification
    .CreditCard<Customer>(c => c.CreditCard.CardNumber);

spec.IsSatisfiedBy(new Customer {CreditCard = new CreditCard {CardNumber = null}}); // return false, because Specification result
spec.IsSatisfiedBy(new Customer {CreditCard = null});   // return false
spec.IsSatisfiedBy(null);   // return false
GitHub