MDiator 0.1.1
dotnet add package MDiator --version 0.1.1
NuGet\Install-Package MDiator -Version 0.1.1
<PackageReference Include="MDiator" Version="0.1.1" />
<PackageVersion Include="MDiator" Version="0.1.1" />
<PackageReference Include="MDiator" />
paket add MDiator --version 0.1.1
#r "nuget: MDiator, 0.1.1"
#:package MDiator@0.1.1
#addin nuget:?package=MDiator&version=0.1.1
#tool nuget:?package=MDiator&version=0.1.1
MDiator π§©
MDiator is a lightweight, extensible in-process messaging library for .NET.
It provides clean abstractions for sending requests and publishing events using a mediator pattern β without the bloat.
Inspired by MediatR, built for performance and clarity.
π Features
Send<TRequest>
: Handle a single request with a typed responsePublish<TEvent>
: Broadcast events to multiple handlers- Auto-registration of handlers via assembly scanning
- No reflection at runtime β uses compiled delegates
- Minimal dependencies
- CancelationToken support
π¦ Installation
Add it to your project (once published to NuGet):
dotnet add package MDiator
Register in Program.cs
services.AddMDiator(typeof(Program).Assembly);
π§ͺ Example Usage Define a Command
public class CreateUserCommand : IMDiatorRequest<string>
{
public string UserName { get; set; }
}
public class CreateUserHandler : IMDiatorHandler<CreateUserCommand, string>
{
public Task<string> Handle(CreateUserCommand command)
=> Task.FromResult($"Created user: {command.UserName}");
}
var result = await mediator.Send(new CreateUserCommand { UserName = "Mostafa" });
π£ Event Example
public class OrderCreatedEvent : IMDiatorEvent
{
public int OrderId { get; set; }
}
public class NotifyTeamHandler : IMDiatorEventHandler<OrderCreatedEvent>
{
public Task Handle(OrderCreatedEvent e)
{
Console.WriteLine($"Notify team about order {e.OrderId}");
return Task.CompletedTask;
}
}
await mediator.Publish(new OrderCreatedEvent { OrderId = 123 });
π Performance Benchmark
MDiator is designed to be lean and fast β with no runtime reflection and minimal allocations.
Hereβs a comparison with MediatR using BenchmarkDotNet:
Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio |
---|---|---|---|---|---|---|---|---|
MDiator_HandleEventAsync | 105.51 ns | 13.308 ns | 0.729 ns | 1.00 | 0.01 | 0.0067 | 56 B | 1.00 |
MediatR_HandleEventAsync | 141.35 ns | 28.874 ns | 1.583 ns | 1.34 | 0.02 | 0.0372 | 312 B | 5.57 |
MDiator_HandleRequestAsync | 84.64 ns | 7.637 ns | 0.419 ns | 1.00 | 0.01 | 0.0200 | 168 B | 1.00 |
MediatR_HandleRequestAsync | 106.22 ns | 31.459 ns | 1.724 ns | 1.25 | 0.02 | 0.0343 | 288 B | 1.71 |
Event Handling β 25% faster β‘ and 82% less memory π
Request Handling β 20% faster β‘ and 42% less memory π
Tested with:
- .NET 9
- Simple
Send()
request + one handler - Scoped resolution via Microsoft.Extensions.DependencyInjection
Product | Versions 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. |
-
net9.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.