Skip to main content

ZeroAlloc.Specification

ZeroAlloc.Specification is a source-generated, zero-allocation implementation of the Specification pattern for .NET 8+.

Why ZeroAlloc.Specification?​

The classic DDD Specification pattern allocates heap objects on every composition — And, Or, and Not return new wrapper class instances, and delegate-based predicates create closures. In hot paths this adds GC pressure.

ZeroAlloc.Specification uses a Roslyn incremental source generator to emit And<TOther>(), Or<TOther>(), and Not() methods directly onto your spec struct. Composed types are concrete readonly struct types — no heap allocation, no closures.

Key Properties​

  • Zero allocations — composed specs are structs, not classes
  • EF Core compatible — every spec exposes ToExpression() returning Expression<Func<T, bool>>
  • Compile-time safety — ZA001–ZA005 diagnostics enforce correct usage
  • Familiar API — fluent (.And(), .Or(), .Not()) and static builder (Spec.And(), Spec.Or(), Spec.Not())
  • .NET 8+ — uses C# 12 features throughout

Packages​

PackagePurpose
ZeroAlloc.SpecificationCore interfaces, combinator structs and the bundled source generator
ZeroAlloc.Specification.GeneratorStandalone source generator, kept for backwards compatibility. New consumers reference only ZeroAlloc.Specification