ZeroAlloc.Mediator
4.1.4
dotnet add package ZeroAlloc.Mediator --version 4.1.4
NuGet\Install-Package ZeroAlloc.Mediator -Version 4.1.4
<PackageReference Include="ZeroAlloc.Mediator" Version="4.1.4" />
<PackageVersion Include="ZeroAlloc.Mediator" Version="4.1.4" />
<PackageReference Include="ZeroAlloc.Mediator" />
paket add ZeroAlloc.Mediator --version 4.1.4
#r "nuget: ZeroAlloc.Mediator, 4.1.4"
#:package ZeroAlloc.Mediator@4.1.4
#addin nuget:?package=ZeroAlloc.Mediator&version=4.1.4
#tool nuget:?package=ZeroAlloc.Mediator&version=4.1.4
ZeroAlloc.Mediator
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 — no constructor dependencies needed for this example
public class CreateOrderHandler : IRequestHandler<CreateOrder, OrderId>
{
public ValueTask<OrderId> Handle(CreateOrder request, CancellationToken ct)
=> ValueTask.FromResult(OrderId.NewId());
}
// 3. Register IMediator with DI (the generator emits services.AddMediator() automatically)
services.AddMediator();
// 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 40–160x faster than MediatR with zero heap allocation on all non-streaming paths (.NET 10.0.4, i9-12900HK, BenchmarkDotNet).
| Operation | ZeroAlloc.Mediator | MediatR | Speedup | Alloc |
|---|---|---|---|---|
| Send | 0.5 ns | 78.3 ns | ~160x | 0 B vs 224 B |
| Send + pipeline | 2.8 ns | 101.8 ns | ~46x | 0 B vs 152 B |
| Send (via IMediator DI) | 5.8 ns | 86.3 ns | ~15x | 0 B vs 224 B |
| Publish (1 handler) | 6.1 ns | 243.8 ns | ~40x | 0 B vs 792 B |
| Publish (multi handler) | 6.6 ns | 332.4 ns | ~51x | 0 B vs 1,032 B |
| Stream (5 items) | 202.8 ns | 654.4 ns | ~3x | 104 B vs 528 B |
The 104 B on the Stream row is the C# compiler's async IAsyncEnumerable<T> state machine for your handler method — every mediator dispatching to such handlers pays this; ZA.Mediator's own stream-dispatch contribution is 0 B. See docs/performance.md for the full benchmark table and zero-allocation design explanation.
Features
- Request/Response — strongly-typed
Sendoverloads per request type - Notifications — sequential or parallel (
[ParallelNotification]) dispatch - Streaming —
IAsyncEnumerable<T>viaCreateStream - Pipeline Behaviors — compile-time inlined middleware chain (logging, validation, etc.)
- Bridge Packages —
WithCache(),WithValidation(),WithResilience(),WithTelemetry()(OpenTelemetry spans + metrics onIRequest<T>.Send),WithAuthorization()([Authorize]-gated dispatch viaZeroAlloc.Authorizationpolicies) - Polymorphic Notifications — base interface handlers are automatically included in concrete notification dispatch
- Analyzer Diagnostics — missing handlers, duplicates, and misconfigurations are build errors/warnings
- Zero Allocation —
ValueTask,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 |
| Authorization | [Authorize]-gated dispatch via ZeroAlloc.Authorization policies |
| 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 | Versions 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. |
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.3)
- ZeroAlloc.Pipeline (>= 1.2.0)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.3)
- ZeroAlloc.Pipeline (>= 1.2.0)
NuGet packages (12)
Showing the top 5 NuGet packages that depend on ZeroAlloc.Mediator:
| Package | Downloads |
|---|---|
|
ZeroAlloc.Outbox
Source-generated transactional outbox for .NET. |
|
|
AI.Sentinel
Security monitoring middleware for IChatClient — prompt injection, hallucination, and operational anomaly detection with an intervention engine. |
|
|
ZeroAlloc.Scheduling.Mediator
ZeroAlloc.Mediator bridge for ZeroAlloc.Scheduling — dispatches jobs via IMediator.Send. |
|
|
ZeroAlloc.Mediator.Validation
Pipeline behavior that validates IRequest<T> via ZeroAlloc.Validation before dispatch. Validation failures surface as Result<T, ValidationError> — no exceptions on the hot path. |
|
|
ZeroAlloc.Saga
Source-generated long-running process orchestration. Multi-step sagas with compensation, FSM via ZeroAlloc.StateMachine, dispatch via ZeroAlloc.Mediator. Native AOT compatible. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 4.1.4 | 170 | 5/18/2026 |
| 4.1.3 | 534 | 5/12/2026 |
| 4.1.2 | 238 | 5/12/2026 |
| 4.1.1 | 488 | 5/7/2026 |
| 4.1.0 | 192 | 5/6/2026 |
| 4.0.0 | 171 | 5/3/2026 |
| 2.1.1 | 99 | 4/29/2026 |
| 2.1.0 | 163 | 4/28/2026 |
| 2.0.1 | 161 | 4/29/2026 |
| 2.0.0 | 96 | 4/28/2026 |
| 1.3.0 | 92 | 4/28/2026 |
| 1.2.1 | 89 | 4/28/2026 |
| 1.2.0 | 96 | 4/24/2026 |
| 1.1.8 | 4,794 | 3/22/2026 |
| 1.1.7 | 100 | 3/20/2026 |
| 1.1.6 | 102 | 3/19/2026 |
| 1.1.5 | 98 | 3/17/2026 |