Skip to content

wikstfre/Mediator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Mediator

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.

How

Setup your commands, queries, events and pipelines using dependency injection. An example:

Implement handler and behavior:

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
  }
}

Register message, handler and behavior

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.

Sending messages

var ping = new PingMessage();

mediator.Send(ping);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages