Table of Contents

Class Specification<T>

Namespace
KYS.EFCore.Library
Assembly
KYS.EFCore.Library.dll

Base class for the Specification pattern: encapsulates a filter criterion for T, plus the related entities to include and whether to track the results.

public abstract class Specification<T>

Type Parameters

T

The type of the object the specification applies to.

Inheritance
Specification<T>
Derived
Inherited Members

Remarks

Use ApplySpecification<TEntity>(DbContext, Specification<TEntity>) to query a DbContext with it, or IsSatisfiedBy(T) to check an object in memory.

Properties

AsNoTracking

Gets a value indicating whether query results should not be tracked by the change tracker.

public bool AsNoTracking { get; }

Property Value

bool

Includes

Gets the navigation properties to eager-load when querying.

public List<Expression<Func<T, object>>> Includes { get; }

Property Value

List<Expression<Func<T, object>>>

Methods

AddInclude(Expression<Func<T, object>>)

Adds a navigation property to eager-load when querying.

public void AddInclude(Expression<Func<T, object>> includeExpression)

Parameters

includeExpression Expression<Func<T, object>>

The expression that selects the navigation property.

And(Specification<T>)

Combines this specification with another one using a logical AND.

public Specification<T> And(Specification<T> other)

Parameters

other Specification<T>

The specification to combine with.

Returns

Specification<T>

A new specification that is satisfied when both specifications are satisfied.

Remarks

The Includes and AsNoTracking settings of the operands are not carried over.

ApplyAsNoTracking()

Marks the specification so that query results are not tracked.

public void ApplyAsNoTracking()

IsSatisfiedBy(T)

Determines whether the specified object satisfies the specification.

public bool IsSatisfiedBy(T entity)

Parameters

entity T

The object to check.

Returns

bool

true if entity satisfies the criterion; otherwise false.

Remarks

The expression from ToExpression() is compiled on every call.

Or(Specification<T>)

Combines this specification with another one using a logical OR.

public Specification<T> Or(Specification<T> other)

Parameters

other Specification<T>

The specification to combine with.

Returns

Specification<T>

A new specification that is satisfied when either specification is satisfied.

Remarks

The Includes and AsNoTracking settings of the operands are not carried over.

ToExpression()

Returns the filter criterion as an expression that can be translated by a LINQ provider.

public abstract Expression<Func<T, bool>> ToExpression()

Returns

Expression<Func<T, bool>>

The filter criterion.