ZeroReflection.Mediator
1.0.4
dotnet add package ZeroReflection.Mediator --version 1.0.4
NuGet\Install-Package ZeroReflection.Mediator -Version 1.0.4
<PackageReference Include="ZeroReflection.Mediator" Version="1.0.4" />
<PackageVersion Include="ZeroReflection.Mediator" Version="1.0.4" />
<PackageReference Include="ZeroReflection.Mediator" />
paket add ZeroReflection.Mediator --version 1.0.4
#r "nuget: ZeroReflection.Mediator, 1.0.4"
#:package ZeroReflection.Mediator@1.0.4
#addin nuget:?package=ZeroReflection.Mediator&version=1.0.4
#tool nuget:?package=ZeroReflection.Mediator&version=1.0.4
ZeroReflection.Mediator
ZeroReflection.Mediator is a high-performance .NET source generator for implementing the Mediator pattern with zero runtime reflection. It provides compile-time code generation for request handling and validation logic in your applications.
Features
- Request/Response handling - Send commands and queries with typed responses
- Validation support - Automatic validation before request handling
- Zero reflection - All dispatch logic generated at compile time
- Optimized dispatching - Switch-based jump table or if/else chains
- Automatic DI registration - Generated extension method for service registration
- AOT-friendly - No runtime code generation or reflection
Getting Started
Installation
Add the NuGet packages to your project:
dotnet add package ZeroReflection.Mediator
dotnet add package ZeroReflection.MediatorGenerator
Basic Usage
1. Define a Request and Handler
using ZeroReflection.Mediator;
// Request with response
public class PingCommand : IRequest<string>
{
public string Message { get; set; }
}
// Handler
public class PingCommandHandler : IRequestHandler<PingCommand, string>
{
public Task<string> Handle(PingCommand request, CancellationToken cancellationToken)
{
return Task.FromResult($"Pong: {request.Message}");
}
}
2. Commands Without Response (Using Unit)
For commands that don't return a value, use the Unit type:
public class AddProductCommand : IRequest<Unit>
{
public string ProductName { get; set; }
}
public class AddProductCommandHandler : IRequestHandler<AddProductCommand, Unit>
{
public Task<Unit> Handle(AddProductCommand request, CancellationToken cancellationToken)
{
// Do something
Console.WriteLine($"Added product: {request.ProductName}");
return Task.FromResult(Unit.Value);
}
}
3. Add Validation (Optional)
public class PingCommandValidator : IValidator<PingCommand>
{
public void Validate(PingCommand request)
{
if (string.IsNullOrWhiteSpace(request.Message))
throw new ArgumentNullException(nameof(request.Message));
}
}
4. Register Services
The source generator automatically creates a RegisterZeroReflectionMediatorHandlers() extension method:
using Microsoft.Extensions.DependencyInjection;
using ZeroReflection.Mediator;
var services = new ServiceCollection();
// Automatically registers IMediator and all handlers/validators
services.RegisterZeroReflectionMediatorHandlers();
var serviceProvider = services.BuildServiceProvider();
5. Use the Mediator
var mediator = serviceProvider.GetRequiredService<IMediator>();
var command = new PingCommand { Message = "Hello" };
var result = await mediator.Send(command);
// result: "Pong: Hello"
Configuration
Disable Code Generation
To disable code generation in a specific project, add this to your .csproj file:
<PropertyGroup>
<EnableZeroReflectionMediatorGeneratedCode>false</EnableZeroReflectionMediatorGeneratedCode>
</PropertyGroup>
When set to false, the source generator will not emit generated code for ZeroReflection.Mediator.
Switch Dispatcher Mode
By default, the generator uses a switch-based dispatcher for optimal performance. To use if/else chains instead:
<PropertyGroup>
<ZeroReflectionMediatorUseSwitchDispatcher>false</ZeroReflectionMediatorUseSwitchDispatcher>
</PropertyGroup>
The switch dispatcher uses a dictionary lookup + switch statement for fast type-based dispatch, while if/else mode uses sequential type comparisons.
How It Works
The source generator:
- Scans your project for classes implementing
IRequestHandler<,>andIValidator<> - Generates a
GeneratedMediatorDispatcherthat handles type-based routing without reflection - Creates a
RegisterZeroReflectionMediatorHandlers()method that registers all handlers and validators - Generates optimized dispatch code using either switch statements or if/else chains
All dispatching happens at compile time - no reflection or dynamic code generation at runtime.
License
MIT
Repository
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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 was computed. 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. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.CSharp (>= 4.7.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.