ZeroReflection.Mediator 1.0.4

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

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:

  1. Scans your project for classes implementing IRequestHandler<,> and IValidator<>
  2. Generates a GeneratedMediatorDispatcher that handles type-based routing without reflection
  3. Creates a RegisterZeroReflectionMediatorHandlers() method that registers all handlers and validators
  4. 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

GitHub

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.0.4 137 11/28/2025
1.0.3 208 11/25/2025
1.0.2 183 11/24/2025
1.0.1 184 11/23/2025
1.0.0 300 11/12/2025