Skip to content

OnixLabs.Core.Specification

matthew edited this page May 27, 2026 · 2 revisions

Specification<T> Class

Represents the base class for the specification pattern: an encapsulated, named, reusable predicate over a subject type T, expressed as a LINQ-compatible Expression<Func<T, bool>> so that the same rule can be evaluated in memory or translated into a database query.

Namespace OnixLabs.Core
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → Specification<T>
Derived CriteriaSpecification<T>

Definition

public abstract class Specification<T>

Type Parameters

Parameter Description
T The underlying type of the subject to which the specification applies.

Remarks

A specification encapsulates a business rule as an object. The rule itself is exposed by the abstract Criteria property as an Expression<Func<T, bool>> rather than a compiled Func<T, bool>. Storing the rule as an expression tree is what makes the pattern useful beyond a simple predicate:

  • In-memory evaluation — call IsSatisfiedBy(T) to test a single subject. The expression is compiled to a delegate on first use and cached, so repeated calls do not recompile.
  • IQueryable translation — because Criteria is an expression tree, it can be passed directly to LINQ providers such as Entity Framework Core (dbSet.Where(spec.Criteria)), where it is translated into SQL and executed in the database rather than in memory.

Specifications are composable. The logical combinators And(Specification<T>), Or(Specification<T>), and Not() each return a new Specification<T> whose Criteria is the corresponding combination of the operand expressions. Because composition happens at the expression-tree level (via the And/Or/Not expression extensions in OnixLabs.Core.Linq), the composed specification remains fully translatable by a LINQ provider.

Two static identity specifications are provided:

  • Empty — always evaluates to true; the identity element for AND composition.
  • Never — always evaluates to false; the identity element for OR composition.

The static And(params IEnumerable<Specification<T>>) and Or(params IEnumerable<Specification<T>>) overloads fold a collection of specifications into one, returning Empty or Never respectively when the collection is empty.

Note The compiled criteria are cached in a Lazy<Func<T, bool>> that uses double-checked locking, so the expression is compiled exactly once even under concurrent access.

To define a concrete specification, either derive from Specification<T> and override Criteria, or construct a CriteriaSpecification<T> directly from an expression.

Examples

Defining specifications by deriving from the base class

using System;
using System.Linq.Expressions;
using OnixLabs.Core;

public sealed record Customer(string Name, int Age, bool IsActive);

public sealed class IsAdultSpecification : Specification<Customer>
{
    public override Expression<Func<Customer, bool>> Criteria => customer => customer.Age >= 18;
}

public sealed class IsActiveSpecification : Specification<Customer>
{
    public override Expression<Func<Customer, bool>> Criteria => customer => customer.IsActive;
}

Evaluating a single subject in memory

Customer customer = new("Alice", 30, IsActive: true);

Specification<Customer> isAdult = new IsAdultSpecification();

bool result = isAdult.IsSatisfiedBy(customer); // true

Composing specifications

Specification<Customer> isAdult = new IsAdultSpecification();
Specification<Customer> isActive = new IsActiveSpecification();

// Logical AND, OR and NOT each produce a new specification.
Specification<Customer> activeAdult = isAdult.And(isActive);
Specification<Customer> minorOrInactive = isAdult.Not().Or(isActive.Not());

bool isActiveAdult = activeAdult.IsSatisfiedBy(new Customer("Bob", 40, true)); // true

Translating to a database query with IQueryable

Because the rule is exposed as an expression tree, it can be handed straight to a LINQ provider for server-side evaluation.

Specification<Customer> activeAdult = new IsAdultSpecification().And(new IsActiveSpecification());

// EF Core translates the criteria expression into SQL.
IQueryable<Customer> query = dbContext.Customers.Where(activeAdult.Criteria);

Folding a collection of specifications

Specification<Customer>[] rules =
[
    new IsAdultSpecification(),
    new IsActiveSpecification()
];

Specification<Customer> all = Specification<Customer>.And(rules);
Specification<Customer> any = Specification<Customer>.Or(rules);

// And(...) over an empty collection returns Specification<Customer>.Empty (always true).
// Or(...)  over an empty collection returns Specification<Customer>.Never (always false).

Constructors

Constructor Description
Specification() Initializes a new instance of the Specification<T> class. Protected; called from a derived type's constructor.

Specification()

protected Specification()

Initializes a new instance of the Specification<T> class, preparing the lazily compiled criteria delegate used by IsSatisfiedBy(T).

Fields

Field Type Description
Empty Specification<T> The specification that always evaluates to true.
Never Specification<T> The specification that always evaluates to false.

Empty

public static readonly Specification<T> Empty

The empty specification, which always evaluates to true. Serves as the identity element for And(params IEnumerable<Specification<T>>).

Never

public static readonly Specification<T> Never

The never specification, which always evaluates to false. Serves as the identity element for Or(params IEnumerable<Specification<T>>).

Properties

Property Type Description
Criteria Expression<Func<T, bool>> Gets the underlying expression criteria of the current specification.

Criteria

public abstract Expression<Func<T, bool>> Criteria { get; }

Gets the underlying expression criteria of the current specification. Returning an expression tree (rather than a compiled delegate) allows the criteria to be composed with other specifications and translated by LINQ providers such as Entity Framework Core into a query.

Methods

Static Methods

Method Returns Description
And(params IEnumerable<Specification<T>>) Specification<T> Combines multiple specifications using a logical AND operation.
Or(params IEnumerable<Specification<T>>) Specification<T> Combines multiple specifications using a logical OR operation.

Instance Methods

Method Returns Description
And(Specification<T>) Specification<T> Combines the current specification with another using a logical AND operation.
Or(Specification<T>) Specification<T> Combines the current specification with another using a logical OR operation.
Not() Specification<T> Negates the current specification's logic using a logical NOT operation.
IsSatisfiedBy(T) bool Evaluates whether the specified subject satisfies the current specification.

And(params IEnumerable<Specification<T>>)

public static Specification<T> And(params IEnumerable<Specification<T>> specifications)

Combines multiple specifications using a logical AND operation.

Parameter Type Description
specifications IEnumerable<Specification<T>> A collection of specifications to combine.

Returns — A combined specification that evaluates to true if all the provided specifications are satisfied, or Empty (always true) if no specifications are provided.

Or(params IEnumerable<Specification<T>>)

public static Specification<T> Or(params IEnumerable<Specification<T>> specifications)

Combines multiple specifications using a logical OR operation.

Parameter Type Description
specifications IEnumerable<Specification<T>> A collection of specifications to combine.

Returns — A combined specification that evaluates to true if any of the provided specifications are satisfied, or Never (always false) if no specifications are provided.

And(Specification<T>)

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

Combines the current specification with another using a logical AND operation.

Parameter Type Description
other Specification<T> The other specification to combine with.

Returns — A combined specification that evaluates to true if both specifications are satisfied; otherwise, false.

Or(Specification<T>)

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

Combines the current specification with another using a logical OR operation.

Parameter Type Description
other Specification<T> The other specification to combine with.

Returns — A combined specification that evaluates to true if either specification is satisfied; otherwise, false.

Not()

public Specification<T> Not()

Creates a specification that negates the current specification's logic using a logical NOT operation.

Returns — A specification that evaluates to true if the current specification is not satisfied; otherwise, false.

IsSatisfiedBy(T)

public bool IsSatisfiedBy(T subject)

Evaluates whether the specified subject satisfies the current specification. On first invocation the Criteria expression is compiled into a delegate, which is then cached for subsequent calls.

Parameter Type Description
subject T The subject to evaluate.

Returns — true if the subject satisfies the specification; otherwise, false.

See Also

ONIXLabs .NET Library


Clone this wiki locally