Mediatrice 1.0.4

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

Mediatrice

Mediatrice is a lightweight implementation of the Mediator design pattern.

Mediatrice enable the use of request/response and commands in synchronous or asynchronous context. The Mediator, through its IMediator interface, dispatches your request and commands based on the contracts you defined.

Current version: 1.0.4

Incoming improvements:

  • create post process behaviors
  • AddBehaviors extension method

Add Mediatrice

In your Program.cs, add the following line :

builder.Services.AddMediatrice();

To add all your classes implementing IRequestHanlder<> or IRequestHandler<,>, use the extension method

builder.Services.AddRequestHandlers();

By default it adds RequestHandlers based on the calling assembly, but you can provide an array :

builder.Services.AddRequestHandlers([Assembly.GetExecutingAssembly()]);

Usage

First you have to declare a class, or record, which implement IRequest or IRequest<> depending if you want the Handle method or IRequestHandler to return a result.

Next, you have to declare a class which implement IRequestHandler<> or IRequestHandler<,>. The first parameter is your Request, the second (if applicable) is the return type.

Here is an example of a IRequest and IRequestHandler returning a Task :

public record AddMeasureCommand(string City, decimal Celsius) : IRequest;  
  
public class AddMeasureHandler : IRequestHandler<AddMeasureCommand>  
{  
    public async Task Handle(AddMeasureCommand request)  
    {  
        Console.WriteLine($"Added measure for {request.City}, {request.Celsius}");  
        await Task.CompletedTask;  
    }  
}

Another example returning a Task<WeatherForecast[]> :

public record GetWeatherForcastRequest(string City) : IRequest<WeatherForecast[]>;  
  
public class GetWeatherHandler : IRequestHandler<GetWeatherForcastRequest, WeatherForecast[]>  
{  
    public async Task<WeatherForecast[]> Handle(GetWeatherForcastRequest request)  
    {  
        Console.WriteLine("GetWeatherForecast for " + request.City);  
          
        var summaries = new[]  
        {  
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };  
          
        var forecast = Enumerable.Range(1, 5).Select(index =>  
            new WeatherForecast  
			(  
                    DateOnly.FromDateTime(DateTime.Now.AddDays(index)),  
                    Random.Shared.Next(-20, 55),  
                    summaries[Random.Shared.Next(summaries.Length)]  
            ))  
            .ToArray();  
          
        return await Task.FromResult(forecast);  
    }  
}

Finally, you can inject IMediator in any class and use its SendAsync(TRequest) method :

var command = new AddMeasureCommand(City: "Montpellier", Celsius: 15m);
await mediator.SendAsync(command);

or

var request = new GetWeatherForcastRequest(City: "Montpellier");
WeatherForecast[] forecasts = await mediator.SendAsync(request);

Behaviors

Behaviors are processes that run before the Request is handled by the IRequestHandler.

You have to add Behaviors to the dependency injection :

builder.Services.AddTransient(typeof(IPipelineBehavior<>), typeof(LoggingBehavior<>));

Behaviors will run in the order that they've been added to the dependency injection.

Here is an example :

public class LoggingBehavior<TRequest> : PipelineBehaviorBase<TRequest>, IPipelineBehavior<TRequest> where TRequest : notnull  
{  
    public async Task ExecuteAsync(TRequest request)  
    {  
        Console.WriteLine("Handled " + request.GetType().Name);  
        await Next(request);  
    }  
}

You have to call Next(request) at the end of the method to ensure that the next behavior runs.

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 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
1.0.4 118 4/1/2026
1.0.3 117 3/30/2026
1.0.2 107 3/30/2026
1.0.1 109 3/30/2026
1.0.0 106 3/29/2026

Publishing repository url