Mediatrice 1.0.4
dotnet add package Mediatrice --version 1.0.4
NuGet\Install-Package Mediatrice -Version 1.0.4
<PackageReference Include="Mediatrice" Version="1.0.4" />
<PackageVersion Include="Mediatrice" Version="1.0.4" />
<PackageReference Include="Mediatrice" />
paket add Mediatrice --version 1.0.4
#r "nuget: Mediatrice, 1.0.4"
#:package Mediatrice@1.0.4
#addin nuget:?package=Mediatrice&version=1.0.4
#tool nuget:?package=Mediatrice&version=1.0.4
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 | Versions 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. |
-
net10.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.5)
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.5)
-
net9.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Publishing repository url