DispatchR.Mediator 1.0.2

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

DispatchR ๐Ÿš€

CI NuGet NuGet

A High-Performance Mediator Implementation for .NET,

** Minimal memory footprint. Blazing-fast execution. **

โšก Key Features

  • ๐Ÿงฉ Built entirely on top of Dependency Injection
  • ๐Ÿšซ Zero runtime reflection after registration
  • ๐Ÿ”ง Choose your handler return type: Task, ValueTask, or Synchronous Method
  • ๐Ÿง  Allocates nothing on the heap โ€” ideal for high-throughput scenarios
  • โšก Outperforms existing solutions in most real-world benchmarks
  • ๐Ÿ”„ Seamlessly compatible with MediatR โ€” migrate with minimal effort

๐Ÿ’ก Tip: If you're looking for a mediator with the raw performance of hand-written code, DispatchR is built for you.

Syntax Comparison: DispatchR vs MediatR

In the following, you will see the key differences and implementation details between MediatR and DispatchR.

โœ… Request Definition

MediatR

public sealed class PingMediatR : IRequest<int> { }

DispatchR

  1. Sending TRequest to IRequest
  2. Precise selection of output for both async and sync handlers
    1. Ability to choose between Task and ValueTask
public sealed class PingDispatchR : IRequest<PingDispatchR, ValueTask<int>> { } 

โœ… Handler Definition

MediatR

public sealed class PingHandlerMediatR : IRequestHandler<PingMediatR, int>
{
    public Task<int> Handle(PingMediatR request, CancellationToken cancellationToken)
    {
        return Task.FromResult(0);
    }
}

DispatchR (Don't change)

public sealed class PingHandlerDispatchR : IRequestHandler<PingDispatchR, ValueTask<int>>
{
    public ValueTask<int> Handle(PingDispatchR request, CancellationToken cancellationToken)
    {
        return ValueTask.FromResult(0);
    }
}

โœ… Pipeline Behavior

MediatR

public sealed class LoggingBehaviorMediat : IPipelineBehavior<PingMediatR, int>
{
    public Task<int> Handle(PingMediatR request, RequestHandlerDelegate<int> next, CancellationToken cancellationToken)
    {
        return next(cancellationToken);
    }
}

DispatchR

  1. Use Chain of Responsibility pattern
public sealed class LoggingBehaviorDispatchR : IPipelineBehavior<PingDispatchR, ValueTask<int>>
{
    public required IRequestHandler<PingDispatchR, ValueTask<int>> NextPipeline { get; set; }

    public ValueTask<int> Handle(PingDispatchR request, CancellationToken cancellationToken)
    {
        return NextPipeline.Handle(request, cancellationToken);
    }
}

๐Ÿ” Summary

  • DispatchR lets the request itself define the return type.
  • No runtime reflection in DispatchR โ€” it's optimized for performance.
  • No static behavior chains โ€” pipelines are chained via DI and handler wiring.
  • Supports void, Task, or ValueTask as return types.

Ideal for high-performance .NET applications.

โšก How DispatchR Achieves High Performance

DispatchR is designed with one goal in mind: maximize performance with minimal memory usage. Here's how it accomplishes that:

๐Ÿ” What Happens Inside the Send Method?

public TResponse Send<TRequest, TResponse>(IRequest<TRequest, TResponse> request,
    CancellationToken cancellationToken) where TRequest : class, IRequest, new()
{
    return serviceProvider
        .GetRequiredService<IRequestHandler<TRequest, TResponse>>()
        .Handle(Unsafe.As<TRequest>(request), cancellationToken);
}

โœ… Only the handler is resolved and directly invoked!

But the real magic happens behind the scenes when DI resolves the handler dependency:

๐Ÿ’ก Tips: We cache the handler using DI, so in scoped scenarios, the object is constructed only once and reused afterward.

services.AddScoped(handlerInterface, sp =>
{
    var pipelines = sp
        .GetServices(pipelinesType)
        .Select(s => Unsafe.As<IRequestHandler>(s)!);

    IRequestHandler lastPipeline = Unsafe.As<IRequestHandler>(sp.GetService(handler))!;
    foreach (var pipeline in pipelines)
    {
        pipeline.SetNext(lastPipeline);
        lastPipeline = pipeline;
    }

    return lastPipeline;
});

โœจ This elegant design chains pipeline behaviors at resolution time โ€” no static lists, no reflection, no magic.

๐Ÿง  Smarter LINQ: Zero Allocation

To further reduce memory allocations, DispatchR uses zLinq, a zero-allocation LINQ implementation, instead of the default LINQ. This means even in heavy pipelines and high-frequency requests, memory remains under control.

Of course, our goal is to stay dependency-free โ€” but for now, I think it's totally fine to rely on this as a starting point!

๐Ÿชด How to use?

It's simple! Just use the following code:

builder.Services.AddDispatchR(typeof(MyCommand).Assembly);

This code will automatically register all pipelines by default. If you need to register them in a specific order, you can either add them manually or write your own reflection logic:

builder.Services.AddDispatchR(typeof(MyCommand).Assembly, withPipelines: false);
builder.Services.AddScoped<IPipelineBehavior<MyCommand, int>, PipelineBehavior>();
builder.Services.AddScoped<IPipelineBehavior<MyCommand, int>, ValidationBehavior>();

๐Ÿ’ก Key Notes:

  1. Automatic pipeline registration is enabled by default
  2. Manual registration allows for custom pipeline ordering
  3. You can implement custom reflection if needed

โœจ How to install?

dotnet add package DispatchR.Mediator --version 1.0.0

๐Ÿงช Bechmark Result:

This benchmark was conducted using MediatR version 12.5.0 and the stable release of Mediator Source Generator, version 2.1.7. Version 3 of Mediator Source Generator was excluded due to significantly lower performance.

1. MediatR vs Mediator Source Generator vs DispatchR With Pipeline

Benchmark Result

2. MediatR vs Mediator Source Generator vs DispatchR Without Pipeline

Benchmark Result

โœจ Contribute & Help Grow This Package! โœจ

We welcome contributions to make this package even better! โค๏ธ

  • Found a bug? ๐Ÿ› โ†’ Open an issue
  • Have an idea? ๐Ÿ’ก โ†’ Suggest a feature
  • Want to code? ๐Ÿ‘ฉ๐Ÿ’ป โ†’ Submit a PR

Let's build something amazing together! ๐Ÿš€

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on DispatchR.Mediator:

Package Downloads
Vexor.Application

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.3.1 7,339 7/11/2026
2.3.0 33,780 5/29/2026
2.2.0 17,145 5/9/2026
2.1.2 166 5/8/2026
2.1.1 138,675 10/8/2025
2.1.0 377 10/6/2025
2.0.1 4,329 9/20/2025
2.0.0 5,076 8/24/2025
1.3.3 7,225 8/3/2025
1.3.2 877 7/9/2025
1.3.1 217 7/7/2025
1.3.0 227 6/26/2025
1.2.1 364 6/11/2025
1.2.0 184 5/31/2025
1.1.0 247 5/19/2025
1.0.2 258 5/4/2025
1.0.0 225 5/4/2025
0.0.4 273 5/2/2025