MinimalMediator.Core 1.6.1-preview-0007

This is a prerelease version of MinimalMediator.Core.
There is a newer version of this package available.
See the version list below for details.
dotnet add package MinimalMediator.Core --version 1.6.1-preview-0007                
NuGet\Install-Package MinimalMediator.Core -Version 1.6.1-preview-0007                
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="MinimalMediator.Core" Version="1.6.1-preview-0007" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MinimalMediator.Core --version 1.6.1-preview-0007                
#r "nuget: MinimalMediator.Core, 1.6.1-preview-0007"                
#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.
// Install MinimalMediator.Core as a Cake Addin
#addin nuget:?package=MinimalMediator.Core&version=1.6.1-preview-0007&prerelease

// Install MinimalMediator.Core as a Cake Tool
#tool nuget:?package=MinimalMediator.Core&version=1.6.1-preview-0007&prerelease                

Minimal Mediator

Github actions Github actions Nuget feed

Minimal Mediator is a lightweight library for implementing the mediator pattern in .NET Minimal APIs.

Lightweight and fast, it is designed to be used in high-performance applications with no overhead.

Minimal Mediator runs in-process without any serialization or reflection.

Native AOT compilation is supported.

Support for publishing/sending messages.

Support for streaming requests and responses based on System.Threading.Channels and IAsyncEnumerable.

The implementation provides a simple middleware pipeline workflow for handling requests and responses that is easy to extend and customize.

Installation

Install the MinimalMediator NuGet package.

dotnet add package MinimalMediator

Initialization

Minimal Mediator supports Microsoft.Extensions.DependencyInjection and Microsoft.Extensions.Hosting for dependency injection and configuration.

There are three possible ways to initialize the mediator services:

  • using Source Generators - automatically by scanning the assembly for handlers and generating the mediator code
builder.Services.AddMinimalMediator(config => config.UseSourceGenerator());
  • using Reflection - automatically by scanning the assembly for handlers and registering them in the service collection
builder.Services.AddMinimalMediator(config => config.UseReflection(typeof(Program)));
  • Manually by registering the mediator and handlers in the service collection
builder.Services.AddMinimalMediator(config =>
{
    config.AddConsumer(typeof(IConsumer<TestMessage>), typeof(Consumer2));
    config.AddMiddleware(typeof(IAfterPublishMiddleware<TestMessage>), typeof(AfterMiddleware1));
    config.AddReceiver(typeof(IReceiver<TestMessage, TestResponse>), typeof(ReceiverTest));
});

Usage

Minimal Mediator supports two types of invocations:

  • Publish/Subscribe - broadcast a message to all consumers
  • Request/Response - send a message and receive a response. This also supports streaming requests and responses.

Publish/Subscribe

Minimal Mediator supports publishing messages to all consumers that implement the IConsumer<TMessage> interface.

public interface IConsumer<TMessage>
{
    Task ConsumeAsync(TMessage message, CancellationToken cancellationToken);
}

The mediator will invoke all consumers in parallel.

Middleware

Because the mediator implements middleware pipelines, it is possible to add middleware that will be invoked before and after the message is published.

This behavior is only valid for the Publish/Subscribe invocation. The Request/Response invocation does not support middleware.

There are three types of middleware:

  • IBeforePublishMiddleware - invoked before the message is published
  • IAfterPublishMiddleware - invoked after the message is published
  • IExceptionHandlerMiddleware - invoked if an exception is thrown during the message publishing

Multiple middleware can be registered of a specific type/message and they will be invoked in the order they are registered.

To control the order of middleware, use the Order property of the MinimalMediatorAttribute class. The order is important only for middleware of the same type.

The presence of this attribute is not required. If it is not present, the middleware will be invoked in the order they are registered.

[MinimalMediator(Order = 2)]
public class AfterMiddleware2 : IAfterPublishMiddleware<TestMessage>
{
    ...
}

Request/Response

Minimal Mediator supports sending and receiving messages and streams. The functionalities are provided by the following interfaces:

  • IReceiver<TMessage, TResponse> - used for sending a message and receiving a response
  • IReceiverStreamAsync<TMessage, TResponse> - used for sending a IAsyncEnumerable<TMessage> stream of messages and receiving a TResponse message
  • IReceiverStreamChannel<TMessage, TResponse> - used for sending a ChannelReader<TMessage> stream of messages and receiving a TResponse message
  • IReceiverConsumeStreamAsync<TMessage, TResponse> - used for sending a TMessage message and receiving a IAsyncEnumerable<TResponse> stream of messages
  • IReceiverConsumeStreamChannel<TMessage, TResponse> - used for sending a TMessage message and receiving a ChannelReader<TResponse> stream of messages

See the sample project and tests for more details.

Service Lifetime

The predefined lifetime of the Publish/Subscribe and Request/Response invocations is Transient. This behavior cannot be changed.

The default lifetime of the IMediator service is Singleton. There is also a Scoped lifetime available that can be enabled from the configuration.

builder.Services.AddMinimalMediator(config => config.UseSourceGenerator(), ServiceLifetime.Scoped);

The mediator will be registered as Scoped but the invocations will still be Transient.

In addition, when using the Scoped lifetime, the mediator creates a new scope for each invocation.

License

MIT

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 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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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 (2)

Showing the top 2 NuGet packages that depend on MinimalMediator.Core:

Package Downloads
MinimalMediator.Reflection

Minimal Mediator Implementation with Streaming and Native AOT support

MinimalMediator

Minimal Mediator Implementation with Streaming and Native AOT support

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.7.3-preview 190 4/12/2024
1.6.8 328 11/15/2023
1.6.6-rc-0002 158 10/26/2023
1.6.4-preview-0007 178 8/25/2023
1.6.1-preview-0007 140 8/21/2023
1.5.1 206 5/23/2023
0.0.0-gf8c178703f 160 5/23/2023