Skip to content

OnixLabs.Core.CriteriaSpecification

matthew edited this page May 27, 2026 · 1 revision

CriteriaSpecification<T> Class

Represents a concrete specification that wraps a supplied Expression<Func<T, bool>> directly, without requiring a dedicated derived class.

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

Definition

public class CriteriaSpecification<T>(Expression<Func<T, bool>> criteria) : Specification<T>

Type Parameters

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

Remarks

CriteriaSpecification<T> is the simplest way to create a specification: pass an expression to the primary constructor and the type exposes it as Criteria. It is useful when a rule does not warrant its own named class.

CriteriaSpecification<T> is also used internally by the And(Specification<T>), Or(Specification<T>), and Not() combinators of Specification<T> — each combinator constructs a CriteriaSpecification<T> whose expression is the composed or negated form of the operand expressions, so composed specifications remain fully translatable by a LINQ provider.

Because CriteriaSpecification<T> inherits all combinators from Specification<T>, a wrapped expression composes with derived specifications and other CriteriaSpecification<T> instances identically.

Examples

Wrapping an expression directly

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

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

Specification<Customer> isAdult = new CriteriaSpecification<Customer>(customer => customer.Age >= 18);

bool result = isAdult.IsSatisfiedBy(new Customer("Alice", 30, true)); // true

Composing wrapped specifications

Specification<Customer> isAdult  = new CriteriaSpecification<Customer>(c => c.Age >= 18);
Specification<Customer> isActive = new CriteriaSpecification<Customer>(c => c.IsActive);

// Wrapped specifications compose just like derived ones.
Specification<Customer> activeAdult = isAdult.And(isActive);

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

Mixing with derived specifications

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

Specification<Customer> isActive = new CriteriaSpecification<Customer>(c => c.IsActive);
Specification<Customer> activeAdult = new IsAdultSpecification().And(isActive);

Constructors

Constructor Description
CriteriaSpecification(Expression<Func<T, bool>>) Initializes a new instance of the CriteriaSpecification<T> class with the specified criteria.

CriteriaSpecification(Expression<Func<T, bool>>)

public CriteriaSpecification(Expression<Func<T, bool>> criteria)

Initializes a new instance of the CriteriaSpecification<T> class that wraps the specified expression criteria.

Parameter Type Description
criteria Expression<Func<T, bool>> The expression criteria to wrap.

Properties

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

Criteria

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

Gets the expression criteria supplied to the constructor. Overrides Specification<T>.Criteria.

See Also

ONIXLabs .NET Library


Clone this wiki locally