Simple in-process messaging with mediator pattern in .NET
Send commands, queries and events, asynchronous dispatching via C# generic variance.
Create a pipeline around your messages with behaviors, easy and verbose registration.
Setup your commands, queries, events and pipelines using dependency injection. An example:
public class PingMessageHandler : IMessageHandler<PingMessage>
{
private readonly ISomeService _someService;
public PingMessageHandler(ISomeService someService)
{
_someService = someService ?? throw new ArgumentNullException(nameof(someService));
}
public async Task Handle(PingMessage message, IMessageContext context)
{
// handle the message
}
}
public class PingMessageBehavior : IMessageBehavior<PingMessage>
{
private readonly ISomeOtherService _someOtherService;
public PingMessageBehavior(ISomeOtherService someOtherService)
{
_someOtherService = someOtherService ?? throw new ArgumentNullException(nameof(someOtherService));
}
public async Task Handle(IMessage message, IMessageContext context, PipelineDelegate next)
{
// do something before the handler is executed
await next().ConfigureAwait(false);
// do something after the handler is executed
}
}
services
.AddMessage<PingMessage>()
.AddMessageHandler<PingMessageHandler>()
.AddMessageBehavior<PingMessageBehavior>();
Behaviors are executed in the order they are added.
Then you are able to use the injected IMediator
interface.
var ping = new PingMessage();
mediator.Send(ping);