Coder Social home page Coder Social logo

minimal-mediator's Introduction

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 stream of messages and receiving a TResponse message
  • IReceiverStreamChannel<TMessage, TResponse> - used for sending a ChannelReader stream of messages and receiving a TResponse message
  • IReceiverConsumeStreamAsync<TMessage, TResponse> - used for sending a TMessage message and receiving a IAsyncEnumerable stream of messages
  • IReceiverConsumeStreamChannel<TMessage, TResponse> - used for sending a TMessage message and receiving a ChannelReader 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

minimal-mediator's People

Contributors

vlada22 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.