ZeroAlloc.Specification.Generator 1.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ZeroAlloc.Specification.Generator --version 1.3.0
                    
NuGet\Install-Package ZeroAlloc.Specification.Generator -Version 1.3.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ZeroAlloc.Specification.Generator" Version="1.3.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ZeroAlloc.Specification.Generator" Version="1.3.0" />
                    
Directory.Packages.props
<PackageReference Include="ZeroAlloc.Specification.Generator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ZeroAlloc.Specification.Generator --version 1.3.0
                    
#r "nuget: ZeroAlloc.Specification.Generator, 1.3.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ZeroAlloc.Specification.Generator@1.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ZeroAlloc.Specification.Generator&version=1.3.0
                    
Install as a Cake Addin
#tool nuget:?package=ZeroAlloc.Specification.Generator&version=1.3.0
                    
Install as a Cake Tool

ZeroAlloc.Specification

NuGet Build License: MIT AOT GitHub Sponsors

Source-generated, zero-allocation specification pattern for .NET 8+.

Multiple packages in this family — see Documentation or NuGet for the full list.

Install

The source generator is bundled into the main package — a single PackageReference is all you need:

dotnet add package ZeroAlloc.Specification

The standalone ZeroAlloc.Specification.Generator package is still published for backwards compatibility with existing direct PackageReferences, but new consumers should reference only ZeroAlloc.Specification.

Example

[Specification]
public readonly partial struct ActiveUserSpec : ISpecification<User>
{
    public bool IsSatisfiedBy(User user) => user.IsActive;
    public Expression<Func<User, bool>> ToExpression() => u => u.IsActive;
}

[Specification]
public readonly partial struct PremiumUserSpec : ISpecification<User>
{
    private readonly decimal _minSpend;
    public PremiumUserSpec(decimal minSpend) => _minSpend = minSpend;

    public bool IsSatisfiedBy(User user) => user.TotalSpend >= _minSpend;
    public Expression<Func<User, bool>> ToExpression()
    {
        var min = _minSpend;
        return u => u.TotalSpend >= min;
    }
}

// Fluent composition — zero allocation
var spec = new ActiveUserSpec().And(new PremiumUserSpec(1000m));

// In-memory
bool result = spec.IsSatisfiedBy(user);

// EF Core — translates to SQL
var users = await dbContext.Users.Where(spec.ToExpression()).ToListAsync();

Performance

Head-to-head vs Ardalis.Specification 8.0 (the de-facto specification library in .NET). .NET 10.0.7, i9-12900HK, BenchmarkDotNet v0.15.4.

Operation Ardalis.Specification ZA.Specification Speedup
Construct composed spec 1,719 ns / 1,248 B 40 ns / 24 B 43× faster, 52× less alloc
Compose two specs 3,959 ns / 2,648 B 22 ns / 24 B 180× faster, 110× less alloc
Evaluate over 100 items (in-memory) 170,862 ns / 4,688 B 150 ns / 0 B 1,136× faster, 0 B alloc

The in-memory-evaluation gap is dramatic because Ardalis pays Expression.Compile() per call. For EF Core / database queries the SQL provider dominates and the overhead is invisible; for in-memory filtering it dominates.

Full methodology + analysis: docs/performance.md.

Features

  • Zero allocations — composed specs are readonly struct values, not heap objects
  • EF Core compatible — every spec exposes ToExpression() returning Expression<Func<T, bool>>
  • Source-generated fluent API — And<TOther>(), Or<TOther>(), Not() added by Roslyn generator
  • Static builder — Spec.And(), Spec.Or(), Spec.Not() for explicit type arguments
  • Compile-time safety — ZA001–ZA005 diagnostics enforce correct partial struct usage
  • .NET 8+, C# 12

Documentation

Page Description
Introduction What it is and why it exists
Getting Started Install and first specification in 5 minutes
Core Concepts ISpecification<T>, structs, stateful vs stateless
Fluent API Generated And/Or/Not methods
Static Builder Spec.And/Or/Not
Expression Composition ToExpression() and EF Core translation
Diagnostics ZA001–ZA004 compile-time errors and fixes
Generator Internals How the Roslyn generator works
Performance Allocation comparison and benchmarks
Cookbook: EF Core Repository Repository pattern integration
Cookbook: Combining Specs Complex compositions
Cookbook: Stateless Caching Cache expression trees for stateless specs

License

MIT — see LICENSE.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.1 97 11/22/2026
2.0.0 64 11/22/2026
1.4.1 214 11/17/2026
1.4.0 632 11/11/2026
1.3.3 304 10/21/2026
1.3.2 100 10/16/2026
1.3.1 99 10/15/2026
1.3.0 92 10/10/2026
1.2.5 107 9/30/2026
1.2.4 101 9/26/2026
1.2.3 115 9/20/2026
1.2.2 114 9/20/2026
1.2.1 114 9/20/2026
1.0.1 146 5/4/2026
0.1.0 147 3/18/2026