WH.SimpleMediator 1.0.4

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

WH.SimpleMediator

Simple mediator implementation in .NET

Supports request/response, commands, queries, notifications and events, async with intelligent dispatching via C# generic variance.

Examples in the wiki.

Installing WH.SimpleMediator

You should install WH.SimpleMediator with NuGet:

Install-Package WH.SimpleMediator

Or via the .NET Core command line interface:

dotnet add package WH.SimpleMediator

Registering with IServiceCollection

WH.SimpleMediator supports Microsoft.Extensions.DependencyInjection directly. To register various WH.SimpleMediator services and handlers:

services.AddSimpleMediator(cfg => cfg.RegisterServicesFromAssemblies(typeof(Startup).Assembly));

To register behaviors:

services.AddSimpleMediator(cfg => {
    cfg.AddPipelineBehavior(typeof(ValidationCommand<,>));
    cfg.AddPipelineBehavior(typeof(LoggerCommand<,>));
});

Request/response

The request/response interface handles both command and query scenarios:

public record ExampleCommand(Guid Id, string Message) : IRequest<string> { }
public record ExampleQuery(Guid Id, string Message) : IRequest<string> { }

Next, create a handler:

public sealed class ExampleHandler : IRequestHandler<ExampleCommand, string>
{
    public Task<string> Handle(ExampleCommand request, CancellationToken cancellationToken)
    {
        return Task.FromResult("Command proccessed successfully");
    }
}
public sealed class ExampleHandler : IRequestHandler<ExampleQuery, string>
{
    public Task<string> Handle(ExampleQuery request, CancellationToken cancellationToken)
    {
        return Task.FromResult("Query executed successfully");
    }
}

Finally, send a message through the mediator:

var response = await mediator.Send(new ExampleCommand(Guid.NewGuid(), "Message"));
Debug.WriteLine(response); // "Command proccessed successfully"
var response = await mediator.Send(new ExampleQuery(Guid.NewGuid(), "Message"));
Debug.WriteLine(response); // "Query executed successfully"

Notifications

For notifications, first create your notification message:

public record ExampleEvent(Guid Id, string Message) : INotification { }

Next, create zero or more handlers for your notification:

public sealed class ExampleEventHandler : INotificationHandler<ExampleEvent>
{
    public Task Handle(ExampleEvent notification, CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
    }
}

Finally, publish your message via the mediator:

await mediator.Publish(new ExampleEvent(Guid.NewGuid(), "Message"));

Behaviors

The behavior interface can be used for executions pre or pos the request handler:

public class ValidationCommand<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
    where TRequest : IRequest<TResponse>
    where TResponse : Result
{
    public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
    {
        //Implement Validation

        return await next(cancellationToken);
    }
}
public class LoggerCommand<TRequest, TResponse>(ILogger<LoggerCommand<TRequest, TResponse>> logger) : IPipelineBehavior<TRequest, TResponse>
    where TRequest : IRequest<TResponse>
    where TResponse : Result
{
    public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
    {
        logger.LogInformation("Start Command");

        var result = await next(cancellationToken);

        logger.LogInformation("End command");

        return result;
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on WH.SimpleMediator:

Package Downloads
WH.SharedKernel

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.4 460 9/18/2025
1.0.3 211 7/15/2025
1.0.2 203 6/29/2025
1.0.1 188 6/26/2025
1.0.0 173 6/25/2025