Olbrasoft.Mediation
10.0.0
dotnet add package Olbrasoft.Mediation --version 10.0.0
NuGet\Install-Package Olbrasoft.Mediation -Version 10.0.0
<PackageReference Include="Olbrasoft.Mediation" Version="10.0.0" />
<PackageVersion Include="Olbrasoft.Mediation" Version="10.0.0" />
<PackageReference Include="Olbrasoft.Mediation" />
paket add Olbrasoft.Mediation --version 10.0.0
#r "nuget: Olbrasoft.Mediation, 10.0.0"
#:package Olbrasoft.Mediation@10.0.0
#addin nuget:?package=Olbrasoft.Mediation&version=10.0.0
#tool nuget:?package=Olbrasoft.Mediation&version=10.0.0
Mediation
A lightweight, high-performance implementation of the mediator design pattern for .NET applications. This library provides a simple and efficient way to implement CQRS (Command Query Responsibility Segregation) patterns and decouple your application components.
๐ Features
- High Performance: Optimized for speed with minimal overhead
- Lightweight: Zero external dependencies except Microsoft.Extensions.DependencyInjection.Abstractions
- Multiple Mediator Implementations: Choose the right mediator for your needs
RequestHandlerMediator- Direct handler resolutionDynamicMediator- Dynamic method invocationReflectionMediator- Reflection-based approachRequestHandlerWrapperMediator- Wrapper-based implementation with caching
- Dependency Injection Ready: Built-in support for Microsoft.Extensions.DependencyInjection
- Multi-Target Framework: Supports .NET 6, 7, 8, 9, 10 and .NET Standard 2.1
- Generic Request/Response: Type-safe request and response handling
- Async/Await Support: Fully asynchronous API
- Comprehensive Testing: 96+ unit tests ensuring reliability
- Automated CI/CD: Continuous integration and deployment to NuGet.org
๐ฆ Installation
Package Manager
Install-Package Olbrasoft.Mediation
.NET CLI
dotnet add package Olbrasoft.Mediation
PackageReference
<PackageReference Include="Olbrasoft.Mediation" Version="10.0.0" />
๐ง Quick Start
1. Define a Request
using Olbrasoft.Mediation.Abstractions;
public class GetUserQuery : IRequest<User>
{
public int UserId { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
2. Create a Handler
using Olbrasoft.Mediation;
public class GetUserHandler : IRequestHandler<GetUserQuery, User>
{
private readonly IUserRepository _userRepository;
public GetUserHandler(IUserRepository userRepository)
{
_userRepository = userRepository;
}
public async Task<User> HandleAsync(GetUserQuery request, CancellationToken cancellationToken)
{
return await _userRepository.GetByIdAsync(request.UserId);
}
}
3. Register Services
using Microsoft.Extensions.DependencyInjection;
using Olbrasoft.Mediation;
var services = new ServiceCollection();
// Register mediation with handler discovery
services.AddMediation(typeof(GetUserHandler).Assembly)
.UseRequestHandlerMediator(); // Choose your preferred mediator
// Register your other services
services.AddScoped<IUserRepository, UserRepository>();
var serviceProvider = services.BuildServiceProvider();
4. Use the Mediator
public class UserController : ControllerBase
{
private readonly IMediator _mediator;
public UserController(IMediator mediator)
{
_mediator = mediator;
}
[HttpGet("{id}")]
public async Task<User> GetUser(int id)
{
var query = new GetUserQuery { UserId = id };
return await _mediator.MediateAsync<User>(query);
}
}
๐ฏ Mediator Types
RequestHandlerMediator (Recommended)
Fast and direct handler resolution through dependency injection.
services.AddMediation(assemblies).UseRequestHandlerMediator();
DynamicMediator
Uses dynamic method invocation for flexibility.
services.AddMediation(assemblies).UseDynamicMediator();
ReflectionMediator
Reflection-based approach for complex scenarios.
services.AddMediation(assemblies).UseReflectionMediator();
RequestHandlerWrapperMediator
Wrapper-based implementation with advanced caching.
services.AddMediation(assemblies).UseRequestHandlerWrapperMediator();
๐ Advanced Usage
Generic Requests
public class GetEntityQuery<T> : IRequest<T> where T : class
{
public int Id { get; set; }
}
public class GetEntityHandler<T> : IRequestHandler<GetEntityQuery<T>, T>
where T : class
{
public async Task<T> HandleAsync(GetEntityQuery<T> request, CancellationToken cancellationToken)
{
// Implementation
}
}
Command Pattern
public class CreateUserCommand : IRequest<int>
{
public string Name { get; set; }
public string Email { get; set; }
}
public class CreateUserHandler : IRequestHandler<CreateUserCommand, int>
{
public async Task<int> HandleAsync(CreateUserCommand request, CancellationToken cancellationToken)
{
// Create user and return ID
return newUserId;
}
}
Multiple Assemblies
services.AddMediation(
typeof(UserHandler).Assembly,
typeof(OrderHandler).Assembly,
typeof(ProductHandler).Assembly
).UseRequestHandlerMediator();
๐๏ธ Architecture
The library follows a clean architecture approach:
- Abstractions: Core interfaces (
IRequest<T>,IRequestHandler<T,R>,IMediator) - Implementations: Various mediator implementations optimized for different scenarios
- Extensions: Dependency injection integration and builder pattern
- Performance: Minimal allocations and optimized execution paths
๐ง Configuration Options
Custom Timeout
The library includes built-in protection against infinite loops in complex generic scenarios:
// Default timeout is 15 seconds for handler registration
services.AddMediation(assemblies); // Uses default timeout
// The library will throw TimeoutException if registration takes too long
Limits and Safety
- Maximum generic type parameters: 10
- Maximum types closing: 100
- Maximum generic type registrations: 125,000
- Registration timeout: 15 seconds
๐งช Testing
The library includes comprehensive test coverage:
dotnet test
# Result: 96 tests passed
Example test:
[Fact]
public async Task Mediator_Should_Handle_Request_Successfully()
{
// Arrange
var services = new ServiceCollection();
services.AddMediation(Assembly.GetExecutingAssembly())
.UseRequestHandlerMediator();
var serviceProvider = services.BuildServiceProvider();
var mediator = serviceProvider.GetRequiredService<IMediator>();
// Act
var result = await mediator.MediateAsync<string>(new TestRequest { Value = "test" });
// Assert
Assert.Equal("Handled: test", result);
}
๐ Performance
Benchmarks show excellent performance characteristics:
- Low Memory Allocation: Minimal GC pressure
- Fast Execution: Optimized resolution paths
- Scalable: Handles thousands of requests efficiently
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Related Projects
- MediatR - The original inspiration
- Microsoft.Extensions.DependencyInjection
๐ Support
Made with โค๏ธ by Olbrasoft
| 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 is compatible. 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 is compatible. 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 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
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.7)
- Olbrasoft.Mediation.Abstractions (>= 10.0.0)
-
net6.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Olbrasoft.Mediation.Abstractions (>= 10.0.0)
-
net7.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Olbrasoft.Mediation.Abstractions (>= 10.0.0)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Olbrasoft.Mediation.Abstractions (>= 10.0.0)
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.7)
- Olbrasoft.Mediation.Abstractions (>= 10.0.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Olbrasoft.Mediation:
| Package | Downloads |
|---|---|
|
Olbrasoft.Data.Cqrs.Common
CQS and CQRS common base classes and interfaces |
|
|
Olbrasoft.Mediation.Benchmarks
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version 10.0.0: Full support for .NET 10, updated all dependencies to latest versions, aligned package version with .NET 10 for clear compatibility. No breaking changes - seamless upgrade from 9.0.1.