OpenMediator 1.1.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package OpenMediator --version 1.1.2
                    
NuGet\Install-Package OpenMediator -Version 1.1.2
                    
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="OpenMediator" Version="1.1.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpenMediator" Version="1.1.2" />
                    
Directory.Packages.props
<PackageReference Include="OpenMediator" />
                    
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 OpenMediator --version 1.1.2
                    
#r "nuget: OpenMediator, 1.1.2"
                    
#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.
#addin nuget:?package=OpenMediator&version=1.1.2
                    
Install OpenMediator as a Cake Addin
#tool nuget:?package=OpenMediator&version=1.1.2
                    
Install OpenMediator as a Cake Tool

codecov

OpenMediator

Alternative for those who do not want to pay for a mediator implementation.

Configuration

In your Program.cs call AddOpenMediator

services.AddOpenMediator(config =>
{
    config.RegisterCommandsFromAssemblies([Assembly.GetExecutingAssembly()]);
});

Usage

1 Create commands

public record CreateUserCommand(int Id, string Name) : ICommand;

2 Call MediatorBus

[ApiController]
[Route("api/user")]
public class UserController(IMediatorBus _mediator) : ControllerBase
{
    [HttpPost()]
    public async Task<IActionResult> CreateUser()
    {
        var command = new CreateUserCommand(1, "UserTest");
        await _mediator.SendAsync(command);

        return Ok();
    }
}

3 Define your use case

public record CreateUserCommandHandler(ILogger<CreateUserCommandHandler> _logger) : ICommandHandler<CreateUserCommand>
{
    public async Task HandleAsync(CreateUserCommand command)
    {
        // Simulate some work
        await Task.Delay(1000);
    }
}

Middleware Configuration

Also you can configure and execute custom middlewares before or after the command.

  1. Define your middleware by implementing the IMiddleware interface:
public class CustomMediatorMiddleware() : IMediatorMiddleware
{
    public async Task ExecuteAsync<TCommand>(TCommand command, Func<Task> next)
        where TCommand : ICommand
    {
        // Do something before the command
        await Task.Delay(500);

        await next();

        // Do something after the command
        await Task.Delay(500);
    }

    public async Task<TResponse> ExecuteAsync<TCommand, TResponse>(TCommand command, Func<Task<TResponse>> next)
        where TCommand : ICommand<TResponse>
    {
        // Do something before the command
        await Task.Delay(500);

        var result = await next();

        // Do something after the command
        await Task.Delay(500);

        return result;
    }
}
  1. Register your middlewares in the AddOpenMediator method:
services.AddOpenMediator(config =>
{
    config.RegisterCommandsFromAssemblies([Assembly.GetExecutingAssembly()]);
    config.RegisterMiddleware<CustomMediatorMiddleware>();
});
Product Compatible and additional computed target framework versions.
.NET 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 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. 
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 OpenMediator:

Package Downloads
OpenMediator.Shared.Integration.Test

Package Description

OpenMediator.Shared.Test

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.3 169 4/20/2025
1.1.2 178 4/13/2025
1.1.1 177 4/13/2025
1.1.0 141 4/13/2025
1.0.0 166 4/10/2025
0.0.5 173 4/10/2025
0.0.4 169 4/6/2025
0.0.3 165 4/6/2025
0.0.2 164 4/6/2025
0.0.1 163 4/6/2025