Olbrasoft.Mediation 10.0.0

dotnet add package Olbrasoft.Mediation --version 10.0.0
                    
NuGet\Install-Package Olbrasoft.Mediation -Version 10.0.0
                    
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="Olbrasoft.Mediation" Version="10.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Olbrasoft.Mediation" Version="10.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Olbrasoft.Mediation" />
                    
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 Olbrasoft.Mediation --version 10.0.0
                    
#r "nuget: Olbrasoft.Mediation, 10.0.0"
                    
#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 Olbrasoft.Mediation@10.0.0
                    
#: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=Olbrasoft.Mediation&version=10.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Olbrasoft.Mediation&version=10.0.0
                    
Install as a Cake Tool

Mediation

License: MIT .NET NuGet Build Publish NuGet

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 resolution
    • DynamicMediator - Dynamic method invocation
    • ReflectionMediator - Reflection-based approach
    • RequestHandlerWrapperMediator - 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

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.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ“ž Support

  • Create an Issue for bug reports or feature requests
  • Check the Wiki for detailed documentation

Made with โค๏ธ by Olbrasoft

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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 Downloads Last Updated
10.0.0 474 12/4/2025
9.0.1 265 8/4/2025
1.1.0 204 8/3/2025
1.0.4 1,013 8/21/2024
1.0.3 188 8/20/2024
1.0.2 215 8/18/2024
1.0.1 200 8/18/2024
1.0.0 204 8/18/2024

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.