Minimal.Mediator 3.0.0

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

Minimal Mediator with Custom Pipelines

This mediator enables the definition of different pipelines for various request types, allowing for greater flexibility and control.

NuGet Package

dotnet add package Minimal.Mediator

Usage

Define a marker interface or base class for each pipeline requests

public interface IRequestA { }

Define requests and request handlers

public sealed class Ping : IRequest<Ping, Pong>, IRequestA
{
    // Implementation details
}

public sealed class PingHandler : IRequestHandler<Ping, Pong>
{
    public Task<Pong> Handle(Ping request, CancellationToken cancellationToken)
    {
        // ...
    }
}

The TRequest generic parameter in the IRequest interface is designed to eliminate the need for C# Reflection in the implementation of the Mediator class. This approach ensures type safety and significantly reduces the runtime overhead commonly associated with reflection-based solutions.

Define middlewares

Custom middleware can be created to process general or specific request types. C#'s generic type constraints (where keyword) can be used to enforce type restrictions.

public sealed class ExceptionHandlingMiddleware<TRequest, TResponse> : IMiddleware<TRequest, TResponse>
{
    public async Task<TResponse> Handle(RequestContext<TRequest> context, IRequestProcessor<TRequest, TResponse> next)
    {
        try
        {
            return await next.Handle(context);
        }
        catch (Exception exp)
        {
            // Exception handling logic
        }
    }
}

public sealed class SpecialMiddleware<TRequest, TResponse> : IMiddleware<TRequest, TResponse>
    where TRequest : ISpecialRequest
{
    // Middleware implementation
}

public sealed class PingMiddleware : IMiddleware<Ping, Pong>
{
    // Middleware implementation
}

Define pipelines

Define Pipelines by using the KeyedPipeline Base Class.

This approach involves defining a uniquely named pipeline class along with a configuration class that implements the IKeyedPipelineConfiguration interface.


internal static class PipelineA
{
    public sealed class Pipeline<TRequest, TResponse> : KeyedPipeline<TRequest, TResponse>
        where TRequest : IRequest<TRequest, TResponse>, IRequestA
    {
        public Pipeline(IServiceProvider serviceProvider)
            : base(serviceProvider, Configuration.PipelineName)
        { }
    }

    public sealed class Configuration : IKeyedPipelineConfiguration
    {
        public static string PipelineName { get; } = "Pipeline_A";

        public static MiddlewareDescriptor[] Middlewares()
        {
            return
            [
                new MiddlewareDescriptor(typeof(ExceptionHandlingMiddleware<,>)),
                new MiddlewareDescriptor(typeof(ValidationMiddleware<,>)),
                new MiddlewareDescriptor(typeof(SpecialMiddleware<,>)),
                new MiddlewareDescriptor(typeof(PingMiddleware), typeof(IMiddleware<Ping, Pong>)),
            ];
        }
    }
}

Register in DI

/// Register Mediator
services.AddMediator();

///Register Pipeline
services.AddKeyedPipeline<PipelineA.Configuration>(typeof(PipelineA.Pipeline<,>));

To automatically register Request-Handlers and Notification-Handlers, consider using the ServiceScan.SourceGenerator package or the Scrutor package if Native AOT is not a concern (code).

If a request of type TRequest does not have a registered pipeline, it will be forwarded directly to its designated handler.

Use IMediator to handle requests

private static async Task Sample(IMediator mediator, CancellationToken cancellationToken)
{
    var request = new Ping() { /* ... */ };
    var response = await mediator.Send(request, cancellationToken);
    // ...
}

You can also utilize the ISender<,> service directly for request handling, ensuring a streamlined and efficient process.

private static async Task Sample(ISender<Ping, Pong> sender, CancellationToken cancellationToken)
{
    var request = new Ping() { /* ... */ };
    var response = await sender.Send(request, cancellationToken);
    // ...
}

Please refer to the sample project for more details.

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

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
3.0.0 157 5/22/2025
2.1.0 153 5/1/2025