ZeroAlloc.Mediator 1.1.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package ZeroAlloc.Mediator --version 1.1.3
                    
NuGet\Install-Package ZeroAlloc.Mediator -Version 1.1.3
                    
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.Mediator" Version="1.1.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ZeroAlloc.Mediator" Version="1.1.3" />
                    
Directory.Packages.props
<PackageReference Include="ZeroAlloc.Mediator" />
                    
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.Mediator --version 1.1.3
                    
#r "nuget: ZeroAlloc.Mediator, 1.1.3"
                    
#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.Mediator@1.1.3
                    
#: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.Mediator&version=1.1.3
                    
Install as a Cake Addin
#tool nuget:?package=ZeroAlloc.Mediator&version=1.1.3
                    
Install as a Cake Tool

ZeroAlloc.Mediator

NuGet Build License: MIT

ZeroAlloc.Mediator is a source-generated, zero-allocation mediator for .NET 8 and .NET 10. It supports request/response, notifications, and streaming without reflection or dynamic dispatch. The source generator eliminates the runtime overhead that reflection-based mediators incur by wiring all dispatch at compile time — no dictionaries, no virtual dispatch, no delegate allocation per request.

Install

dotnet add package ZeroAlloc.Mediator

The generator package must also be added as an analyzer:

<PackageReference Include="ZeroAlloc.Mediator.Generator" Version="*" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />

Example

// 1. Define a request record and its expected response type
public readonly record struct CreateOrder(string Product, int Qty) : IRequest<OrderId>;

// 2. Implement a handler — constructor-injected dependencies are supported
public class CreateOrderHandler(IOrderRepository repo) : IRequestHandler<CreateOrder, OrderId>
{
    public async ValueTask<OrderId> Handle(CreateOrder request, CancellationToken ct)
    {
        var id = await repo.InsertAsync(request.Product, request.Qty, ct);
        return new OrderId(id);
    }
}

// 3. Register IMediator with DI (the generator emits MediatorService automatically)
services.AddSingleton<IMediator, MediatorService>();

// 4. Send the request and use the result — fully strongly-typed, zero allocation
public class OrderController(IMediator mediator)
{
    public async Task<IResult> PlaceOrder(CreateOrder cmd, CancellationToken ct)
    {
        var id = await mediator.Send(cmd, ct);   // returns OrderId, no boxing
        return Results.Created($"/orders/{id}", id);
    }
}

Performance

ZeroAlloc.Mediator is 26–65x faster than MediatR with zero heap allocation on all synchronous paths (.NET 10, BenchmarkDotNet).

Operation ZeroAlloc.Mediator MediatR Speedup Alloc
Send ~1.9 ns ~75 ns ~40x 0 B vs 224 B
Send + pipeline ~3.0 ns ~77 ns ~26x 0 B vs 152 B
Publish (1 handler) ~5.9 ns ~222 ns ~38x 0 B vs 792 B
Publish (3 handlers) ~5.3 ns ~299 ns ~57x 0 B vs 1,032 B
Stream (per item) ~149 ns ~450 ns ~3x 104 B vs 528 B

See docs/performance.md for the full benchmark table and zero-allocation design explanation.

Features

  • Request/Response — strongly-typed Send overloads per request type
  • Notifications — sequential or parallel ([ParallelNotification]) dispatch
  • StreamingIAsyncEnumerable<T> via CreateStream
  • Pipeline Behaviors — compile-time inlined middleware chain (logging, validation, etc.)
  • Polymorphic Notifications — base interface handlers are automatically included in concrete notification dispatch
  • Analyzer Diagnostics — missing handlers, duplicates, and misconfigurations are build errors/warnings
  • Zero AllocationValueTask, readonly record struct, static dispatch, no closures
  • Native AOT Compatible — no reflection at runtime; all dispatch is resolved at compile time by the source generator

Documentation

Page Description
Getting Started Install and send your first request in five minutes
Requests & Handlers Commands, queries, Unit responses, dispatch
Notifications Events: sequential, parallel, polymorphic handlers
Streaming IAsyncEnumerable<T> for large result sets
Pipeline Behaviors Compile-time middleware: logging, validation, caching
Dependency Injection DI containers, IMediator, factory delegates
Diagnostics ZAM001–ZAM007 compiler error reference with fixes
Performance Zero-alloc internals, benchmark results, Native AOT
Advanced Patterns Error handling, cancellation, scoped behaviors
Testing Unit-test handlers, behaviors, and notifications

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • 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
1.1.8 77 3/22/2026
1.1.7 80 3/20/2026
1.1.6 82 3/19/2026
1.1.5 78 3/17/2026
1.1.4 76 3/17/2026
1.1.3 83 3/17/2026
1.1.2 83 3/17/2026
1.1.1 102 3/16/2026
1.1.0 105 3/16/2026
0.1.5 111 3/16/2026
0.1.4 94 3/15/2026