MVFC.Mediator
2.0.1
See the version list below for details.
dotnet add package MVFC.Mediator --version 2.0.1
NuGet\Install-Package MVFC.Mediator -Version 2.0.1
<PackageReference Include="MVFC.Mediator" Version="2.0.1" />
<PackageVersion Include="MVFC.Mediator" Version="2.0.1" />
<PackageReference Include="MVFC.Mediator" />
paket add MVFC.Mediator --version 2.0.1
#r "nuget: MVFC.Mediator, 2.0.1"
#:package MVFC.Mediator@2.0.1
#addin nuget:?package=MVFC.Mediator&version=2.0.1
#tool nuget:?package=MVFC.Mediator&version=2.0.1
MVFC.Mediator
A high-performance, zero-allocation dispatcher implementation of the Mediator pattern for .NET 9 and 10. Centralize your business logic, decouple components, and scale with confidence.
Motivation
In complex .NET applications, direct dependencies between services often lead to:
- Tight Coupling: Difficult to test and maintain components.
- Boilerplate: Repetitive logic for validation and orchestration.
- Poor Scalability: Hard to evolve systems as they grow.
- Performance Overhead: Common libraries often introduce hidden allocations and reflection costs.
MVFC.Mediator solves this by providing a lightweight, ultra-fast mediator with a zero-allocation dispatcher pattern. It uses static generic caching and advanced IL techniques to ensure that your message routing is as fast as a direct method call.
Architecture
- Zero-Allocation Dispatchers: Uses
Volatile.ReadandInterlockedfor thread-safe, reflection-free handler invocation. - Fluent Integration: Seamlessly connects with
Microsoft.Extensions.DependencyInjection. - Validation-First: Built-in support for
FluentValidationwith parallel execution for multiple validators. - Fan-out Support: Native notification system for broadcasting events to multiple handlers.
Installation
Install the package via NuGet:
dotnet add package MVFC.Mediator
Quick Start
1. Register with Dependency Injection
builder.Services.AddMediator();
2. Basic Command (No Response)
// Define the command
public record DeleteUserCommand(Guid UserId) : ICommand;
// Implement the handler
public class DeleteUserHandler : ICommandHandler<DeleteUserCommand>
{
public ValueTask Handle(DeleteUserCommand command, CancellationToken ct)
{
// Logic to delete user
return ValueTask.CompletedTask;
}
}
// Usage
await mediator.Send(new DeleteUserCommand(uid));
3. Command with Response & Validation
// Response model
public record UserCreated(Guid Id);
// Command with TResponse
public record CreateUser(string Name, string Email) : ICommand<UserCreated>;
// Validator (executed automatically if registered)
public class CreateUserValidator : AbstractValidator<CreateUser>
{
public CreateUserValidator()
{
RuleFor(x => x.Name).NotEmpty().MinimumLength(3);
RuleFor(x => x.Email).EmailAddress();
}
}
// Handler
public class CreateUserHandler : ICommandHandler<CreateUser, UserCreated>
{
public ValueTask<UserCreated> Handle(CreateUser command, CancellationToken ct)
{
return new ValueTask<UserCreated>(new UserCreated(Guid.NewGuid()));
}
}
// Usage
var result = await mediator.Send<CreateUser, UserCreated>(new CreateUser("John", "john@test.com"));
4. Query (Read Logic)
public record GetUserQuery(Guid Id) : IQuery<UserDto>;
public class GetUserHandler : IQueryHandler<GetUserQuery, UserDto>
{
public async ValueTask<UserDto> Handle(GetUserQuery query, CancellationToken ct)
{
return await _repository.GetById(query.Id);
}
}
// Usage
var user = await mediator.Query<GetUserQuery, UserDto>(new GetUserQuery(uid));
5. Notifications (Event Broadcasting)
public record UserRegistered(Guid Id) : INotification;
// Multiple handlers for the same notification
public class LoggingHandler : INotificationHandler<UserRegistered> { ... }
public class WelcomeEmailHandler : INotificationHandler<UserRegistered> { ... }
// Usage (broadcasting to all handlers)
await mediator.Publish(new UserRegistered(uid));
Performance
Built for high-throughput scenarios, benchmarked on .NET 9.
BenchmarkDotNet v0.15.4, Windows 11 (10.0.26200.6901)
12th Gen Intel Core i5-12500H 2.50GHz, 1 CPU, 16 logical and 12 physical cores
.NET SDK 9.0.302
[Host] : .NET 9.0.7 (9.0.7, 9.0.725.31616), X64 RyuJIT x86-64-v3
Job-NTRUNJ : .NET 9.0.7 (9.0.7, 9.0.725.31616), X64 RyuJIT x86-64-v3
IterationCount=5 WarmupCount=3
| Method | Mean | Error | StdDev | Gen0 | Allocated |
|---|---|---|---|---|---|
| 'Individual HTTP request simulation' | 1.364 μs | 0.2325 μs | 0.0604 μs | 0.4272 | 3.97 KB |
| 'Multiple parallel requests simulation' | 13.688 μs | 2.2724 μs | 0.3517 μs | 4.3335 | 40.15 KB |
API Reference
| Interface | Method | Use Case |
|---|---|---|
ICommand |
Send(command) |
Action without return (side effects). |
ICommand<T> |
Send<C, T>(command) |
Action with return. |
IQuery<T> |
Query<Q, T>(query) |
Data retrieval. |
INotification |
Publish(event) |
Broadcasting events (one-to-many). |
Project Structure
src/
MVFC.Mediator/ # Core logic and Dispatchers
playground/
Playground.Api/ # Usage examples and Integration tests
tests/
MVFC.Mediator.Tests # Unit and Architecture tests
Requirements
- .NET 9 or .NET 10
Contributing
See CONTRIBUTING.md.
License
| 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 is compatible. 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. |
-
net10.0
- FluentValidation (>= 12.1.1)
- Microsoft.Extensions.DependencyInjection (>= 10.0.5)
-
net9.0
- FluentValidation (>= 12.1.1)
- Microsoft.Extensions.DependencyInjection (>= 9.0.14)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.