MitMediator 6.0.0
See the version list below for details.
dotnet add package MitMediator --version 6.0.0
NuGet\Install-Package MitMediator -Version 6.0.0
<PackageReference Include="MitMediator" Version="6.0.0" />
<PackageVersion Include="MitMediator" Version="6.0.0" />
<PackageReference Include="MitMediator" />
paket add MitMediator --version 6.0.0
#r "nuget: MitMediator, 6.0.0"
#:package MitMediator@6.0.0
#addin nuget:?package=MitMediator&version=6.0.0
#tool nuget:?package=MitMediator&version=6.0.0
MitMediator
Fast mediator for handling requests, commands, notifications, and streams with ValueTask and ordered pipelines
β¨ Features
- Supports
IRequest<TResponse>
andIRequest
(void-style) - Custom handlers via
IRequestHandler<,>
andIRequestHandler<>
- Supports both
ValueTask
(for modern, efficient handlers) andTask
(for compatibility with MediatR-style handlers) - Enables middleware-style pipelines using
IPipelineBehavior<TRequest, TResponse>
. A pipeline behavior can be defined for all or specific request types. - Supports
INotificationHandler
with serial and parallel publishing - Ordered execution of pipeline behaviors
- Simple registration through
AddMitMediator()
or assembly scanning - Supports
IStreamRequestHandle
andIStreamPipelineBehavior
π Getting Started
Installation
dotnet add package MitMediator -v 6.0.0
Example Usage
Simple application with PingRequest, PingRequestHandler and two behaviors.
using Microsoft.Extensions.DependencyInjection;
using MitMediator;
var services = new ServiceCollection();
services
.AddMitMediator(typeof(PingRequestHandler).Assembly)
.AddTransient(typeof(IPipelineBehavior<,>), typeof(HeightBehavior<,>))
.AddTransient(typeof(IPipelineBehavior<,>), typeof(LowBehavior<,>));
var provider = services.BuildServiceProvider();
var mediator = provider.GetRequiredService<IMediator>();
//HeightBehavior: Handling PingRequest
//LowBehavior: Handling PingRequest
//PingRequestHandler: Pong
//LowBehavior: Handled PingRequest
//HeightBehavior: Handled PingRequest
string result = await mediator.SendAsync<PingRequest, string>(new PingRequest(), CancellationToken.None);
Console.WriteLine(result); //Pong result
public class PingRequest : IRequest<string> { }
public class PingRequestHandler : IRequestHandler<PingRequest, string>
{
public ValueTask<string> HandleAsync(PingRequest request, CancellationToken cancellationToken)
{
Console.WriteLine("PingRequestHandler: Pong");
return ValueTask.FromResult("Pong result");
}
}
public class LowBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse>
{
public async ValueTask<TResponse> HandleAsync(TRequest request, IRequestHandlerNext<TRequest, TResponse> next, CancellationToken cancellationToken)
{
Console.WriteLine($"LowBehavior: Handling {typeof(TRequest).Name}");
var result = await next.InvokeAsync(request, cancellationToken);
Console.WriteLine($"LowBehavior: Handled {typeof(TRequest).Name}");
return result;
}
}
public class HeightBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse>
{
public async ValueTask<TResponse> HandleAsync(TRequest request, IRequestHandlerNext<TRequest, TResponse> next, CancellationToken cancellationToken)
{
Console.WriteLine($"HeightBehavior: Handling {typeof(TRequest).Name}");
var result = await next.InvokeAsync(request, cancellationToken);
Console.WriteLine($"HeightBehavior: Handled {typeof(TRequest).Name}");
return result;
}
}
To use Task
instead of ValueTask
, use MitMediator.Tasks
namespase.
π Migrating from MediatR
You can reuse your existing handlers with minimal modifications β just update the namespaces and registration calls
- Add the
MitMediator
packagedotnet add package MitMediator -v 6.0.0
- In your request files, replace the namespace
MediatR
withMitMediator
- In your request handler files, replace the namespace
MediatR
withMitMediator
(andMitMediator.Tasks
forTask
result) - Update your dependency injection setup: replace
.AddMediatR(...)
with.AddMitMediator()
- If you're implementing
INotificationHandler
, useValueTask
instead ofTask
- If you're implementing
IPipelineBehavior
, useValueTask
instead ofTask
andIRequestHandlerNext<TRequest, TResponse>
instead ofRequestHandlerDelegate<TResponse>
. Usenext.InvokeAsync(request, cancellationToken)
for next pipe - For handlers with void result, use
Task<Unit>
instead ofTask
(returnUnit.Value
) - (Optional) Change all
mediator.Send(request, ct)
tomediator.SendAsync<TRequset, TResponse>(request, ct)
(ormediator.Send<TRequset, TResponse>(request, ct)
forTask
result) - Build and run your project β youβre all set!
Use
SendAsync<TRequset, TResponse>(request, ct)
for best performance orSend(request, ct)
for backward compatibility with MediatR-style semantics
MitMediator is designed to feel familiar for those coming from MediatR. Core concepts like IRequest, IRequestHandle, and pipeline behaviors are preserved β but with a cleaner interface and support for ValueTask out of the box.
π Comparison: MitMediator vs. MediatR
Performance
Mediator | Method | Mean (ns) | Allocated (B) |
---|---|---|---|
MediatR | Send (return result) | 261.2 | 384 |
MitMediator | SendAsync (return result) | 107.6 | 0 |
MediatR | Send (return result, use behaviors) | 456.7 | 864 |
MitMediator | SendAsync (return result, use behaviors) | 101.6 | 0 |
MediatR | Send (Return void) | 229.5 | 312 |
MitMediator | SendAsync (Return void) | 101.0 | 0 |
MediatR | Publish | 379.1 | 592 |
MitMediator | PublishAsync | 113.6 | 32 |
MediatR | CreateStream (return stream, use behavior) | 1,447.0 | 1200 |
MitMediator | CreateStream (return stream, use behavior) | 340.7 | 120 |
Features
Feature | MitMediator | MediatR |
---|---|---|
Return types | ValueTask (default, allocation-friendly) |
Task (standard async support) |
Send methods | Strongly typed requests (SendAsync<TRequest, TResponse> ) |
Loosely typed requests (Send(request) ) |
DI Registration | AddMitMediator() with optional assembly scanning |
AddMediatR() with assemblies explicitly specified |
Extensibility | Designed for lightweight extension and customization | More opinionated; extensibility requires deeper integration |
Notification publishing | Serial and parallel | Only serial out of the box |
Performance Focus | Async-first, zero-allocation for ValueTask |
Flexible but not optimized for ValueTask |
License & Availability | MIT | Reciprocal Public License 1.5 (RPL1.5) and commercial license |
π§ͺ Testing
This project includes comprehensive unit tests with 100% code coverage. All tests are included in the repository
π§© Extensions
MitMediator.AutoApi - automatic creation of API endpoints for requests
π License
MIT
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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. |
-
net6.0
- Microsoft.Extensions.DependencyInjection (>= 6.0.0)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on MitMediator:
Package | Downloads |
---|---|
MitMediator.AutoApi.Abstractions
Minimal API registration for MitMediator |
|
MitMediator.AppAuthorize
Extension for MitMediator that simplifies authentication and authorization |
|
MitMediator.InMemoryCache
An attribute-driven in-memory caching extension for the MitMediator |
GitHub repositories
This package is not used by any popular GitHub repositories.
v6.0.0
Supports IRequest<TResponse> and IRequest (void-style)
Custom handlers via IRequestHandler<,> and IRequestHandler<>
Supports both ValueTask (for modern, efficient handlers) and Task (for compatibility with MediatR-style handlers)
Enables middleware-style pipelines using IPipelineBehavior<TRequest, TResponse>. A pipeline behavior can be defined for all or specific request types.
Supports INotificationHandler with serial and parallel publishing
Ordered execution of pipeline behaviors
Simple registration through AddMitMediator() or assembly scanning
Supports IStreamRequestHandle and IStreamPipelineBehavior