Repository navigation
OnixLabs.Core.Specification
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> |
public abstract class Specification<T>| Parameter | Description |
|---|---|
T |
The underlying type of the subject to which the specification applies. |
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. -
IQueryabletranslation — becauseCriteriais 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 totrue; the identity element for AND composition. -
Never— always evaluates tofalse; 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.
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;
}Customer customer = new("Alice", 30, IsActive: true);
Specification<Customer> isAdult = new IsAdultSpecification();
bool result = isAdult.IsSatisfiedBy(customer); // trueSpecification<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)); // trueBecause 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);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).| Constructor | Description |
|---|---|
Specification() |
Initializes a new instance of the Specification<T> class. Protected; called from a derived type's constructor. |
protected Specification()Initializes a new instance of the Specification<T> class, preparing the lazily compiled criteria delegate used by IsSatisfiedBy(T).
| Field | Type | Description |
|---|---|---|
Empty |
Specification<T> |
The specification that always evaluates to true. |
Never |
Specification<T> |
The specification that always evaluates to false. |
public static readonly Specification<T> EmptyThe empty specification, which always evaluates to true. Serves as the identity element for And(params IEnumerable<Specification<T>>).
public static readonly Specification<T> NeverThe never specification, which always evaluates to false. Serves as the identity element for Or(params IEnumerable<Specification<T>>).
| Property | Type | Description |
|---|---|---|
Criteria |
Expression<Func<T, bool>> |
Gets the underlying expression criteria of the current specification. |
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.
| 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. |
| 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. |
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.
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.
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.
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.
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.
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.
ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions