MVFC.Mediator 2.0.1

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

MVFC.Mediator

🇧🇷 Leia em Português

CI codecov License Platform NuGet Version

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.Read and Interlocked for thread-safe, reflection-free handler invocation.
  • Fluent Integration: Seamlessly connects with Microsoft.Extensions.DependencyInjection.
  • Validation-First: Built-in support for FluentValidation with 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

Apache-2.0

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 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. 
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
3.3.0 130 5/22/2026
3.2.0 179 4/5/2026
3.1.0 104 4/1/2026
2.0.2 99 3/21/2026
2.0.1 93 3/20/2026
2.0.0 91 3/20/2026
1.1.0 311 11/16/2025
1.0.1 207 10/27/2025
1.0.0 198 10/27/2025