Anton_Christiansen.Mediator.Test
0.0.10.33
dotnet add package Anton_Christiansen.Mediator.Test --version 0.0.10.33
NuGet\Install-Package Anton_Christiansen.Mediator.Test -Version 0.0.10.33
<PackageReference Include="Anton_Christiansen.Mediator.Test" Version="0.0.10.33" />
<PackageVersion Include="Anton_Christiansen.Mediator.Test" Version="0.0.10.33" />
<PackageReference Include="Anton_Christiansen.Mediator.Test" />
paket add Anton_Christiansen.Mediator.Test --version 0.0.10.33
#r "nuget: Anton_Christiansen.Mediator.Test, 0.0.10.33"
#:package Anton_Christiansen.Mediator.Test@0.0.10.33
#addin nuget:?package=Anton_Christiansen.Mediator.Test&version=0.0.10.33
#tool nuget:?package=Anton_Christiansen.Mediator.Test&version=0.0.10.33
Beta stage - Mediator
About
- Request handling
- Request pipelines are fully navigational
- Pipeline behaviours
- Ordering of pipelines behaviours
- Notifications
- Source generator
- Minimal configurations
See more detailed information at my github: https://github.com/Anton-Christiansen/Mediator
Limitations
1)
There can only be one handler for every request in the request pipeline, since source generated extension methods will have conflict and cause compile-time errors.
2)
There is an expectation that the Mediator instance will be in the outer layer of the application and sending request inward, in order to see all handlers and pipelines in a multi-project application, and to generate the necessary code to wire each and every request.
Requests
Use Mediator class to send request with the method SendAsync. Extension methods will be generated for every handler.
The extension methods are fully navigational, with a method containing the exact pipeline for your every request.
Example of source generated SendAsync Extension method:
public static ValueTask<Application.Handlers.Add.Response> SendAsync(this Mediator mediator, Application.Handlers.Add.Request request, CancellationToken cancellationToken = default)
{
IServiceScope? scope = mediator.Configurations.UseScope ? mediator.Services.CreateScope() : null;
var sp = scope?.ServiceProvider ?? mediator.Services;
try
{
if (mediator.Configurations.UsePipelines)
{
var loggingBehaviour = sp.GetRequiredService<Application.Behaviours.LoggingBehaviour<Application.Handlers.Add.Request, Application.Handlers.Add.Response>>();
var validationBehaviour = sp.GetRequiredService<Application.Behaviours.ValidationBehaviour<Application.Handlers.Add.Request, Application.Handlers.Add.Response>>();
var transactionBehaviour = sp.GetRequiredService<Application.Behaviours.TransactionBehaviour<Application.Handlers.Add.Request, Application.Handlers.Add.Response>>();
var handler = sp.GetRequiredService<Application.Handlers.Add.Handler>();
return loggingBehaviour.ExecuteAsync(request, (a) =>
validationBehaviour.ExecuteAsync(a, (b) =>
transactionBehaviour.ExecuteAsync(b, (c) =>
handler.HandleAsync(c, cancellationToken),
cancellationToken),
cancellationToken),
cancellationToken);
}
else
{
var handler = sp.GetRequiredService<Application.Handlers.Add.Handler>();
return handler
.HandleAsync(request, cancellationToken);
}
}
finally
{
scope?.Dispose();
}
}
Pipelines behaviours
Pipeline behaviours can be configured in 2 parts:
With the
IPipelineBehaviour<TRequest>,IPipelineBehaviour<TRequest, TResponse>interfaceswith the
IPipelineHandlerBehaviour<THandler, TRequest> where THandler : IRequestHandler<TRequest>,IPipelineHandlerBehaviour<THandler, TRequest, TResponse> where THandler : IRequestHandler<TRequest, TResponse>Will be applied to every pipeline matching it's type arguments
Will be applied to every pipeline with a handler of type
THandler
Pipeline behaviour ordering
The pipeline behaviours are being ordered in 2 parts.
- How derived the handler is from the
IRequestHandler<TRequest>,IRequestHandler<TRequest, TResponse>. - Alphabetic
Notifications
Use the interface INotifier to send notifications with NotifyAsync<TNotification>(TNotification notification) where TNotification : INotification method.
Interfaces
Request and Notifications
IRequestHandler<TRequest>IRequestHandler<TRequest, TResponse>INotificationHandler<TNotification> where TNotification : INotification
Pipeline behaviours
IPipelineBehaviour<TRequest>IPipelineBehaviour<TRequest, TResponse>
Pipeline behaviours with handler type
IPipelineHandlerBehaviour<THandler<TRequest>, TRequest>IPipelineHandlerBehaviour<THandler<TRequest, TResponse>, TRequest, TResponse>
Examples
Considering the following example, that will log every request and response.
using Mediator.Interfaces;
using Microsoft.Extensions.Logging;
namespace Application.Behaviours;
public class LoggingBehaviour<TRequest>(ILogger<LoggingBehaviour<TRequest>> logger) : IPipelineBehaviour<TRequest>
{
public async Task ExecuteAsync(TRequest request, Func<TRequest, Task> next, CancellationToken cancellationToken)
{
logger.LogInformation(request?.ToString());
await next(request);
}
}
public class LoggingBehaviour<TRequest, TResponse>(ILogger<LoggingBehaviour<TRequest, TResponse>> logger) : IPipelineBehaviour<TRequest, TResponse>
{
public async Task<TResponse> ExecuteAsync(TRequest request, Func<TRequest, Task<TResponse>> next, CancellationToken cancellationToken)
{
logger.LogInformation(request?.ToString());
var response = await next(request);
logger.LogInformation(response?.ToString());
return response;
}
}
Dependency injection
Add to dependency injection with the extension method AddMediator of IServiceCollection, that takes an Action to register handlers:
builder.Services.AddMediator(mediatorBuilder =>
{
mediatorBuilder
.AddHandlers()
.AddNotifications()
.AddPipelines();
});
Source generated code will extend MediatorBuilder with AddHandlers(), AddNotifications(), AddPipelines() methods that will add the respective handlers to dependency injection.
Scoped request
You can configure mediator to automatically create a IServiceScope to use for resolving dependencies for each request,
by using UseScopedRequest() method on MediatorBuilder in your
dependency injection configuration. Practical for desktop and console applications.
builder.Services.AddMediator(mediatorBuilder =>
{
mediatorBuilder
.AddHandlers()
.AddNotifications()
.AddPipelines()
.UseScopedRequest(); // here
});
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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
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 |
|---|---|---|
| 0.0.10.33 | 711 | 12/3/2025 |
| 0.0.10.32 | 696 | 12/2/2025 |
| 0.0.10.31 | 692 | 12/2/2025 |
| 0.0.10.30 | 285 | 11/10/2025 |
| 0.0.10.29 | 287 | 11/10/2025 |
| 0.0.10.28 | 288 | 11/10/2025 |
| 0.0.10.27 | 268 | 11/10/2025 |
| 0.0.10.26 | 226 | 11/9/2025 |
| 0.0.10.25 | 230 | 11/9/2025 |
| 0.0.10.24 | 226 | 11/9/2025 |
| 0.0.10.23 | 227 | 11/9/2025 |
| 0.0.10.22 | 235 | 11/9/2025 |
| 0.0.10.21 | 215 | 10/30/2025 |
| 0.0.10.20 | 210 | 10/27/2025 |
| 0.0.10.19 | 208 | 10/26/2025 |
| 0.0.10.18 | 209 | 10/26/2025 |
| 0.0.10.17 | 211 | 10/26/2025 |
| 0.0.10.16 | 131 | 10/25/2025 |
| 0.0.10.15 | 125 | 10/25/2025 |
| 0.0.10.14 | 127 | 10/25/2025 |