zarusz/SlimMessageBus

More rich publish / send / consume / handler interceptors that could be injected from DI

zarusz opened this issue · 1 comments

Currently, we have just simple OnMessageArrived / OnMessageProduced delegates that can used as a hook into some message processing steps. We cannot introduce other required dependencies (from DI) with these delegates nor do they react to a particular type of massage.

SMB could have the following set of interfaces that the runtime attempt to resolve from DI and execute their implementations if the user where to register them:

    public interface IPublishInterceptor<in TMessage>
    {
        Task OnHandle(TMessage message, CancellationToken cancellationToken, Func<Task> next, IMessageBus bus, string path, IDictionary<string, object> headers);
    }

    public interface ISendInterceptor<in TRequest, TResponse>
    {
        Task<TResponse> OnHandle(TRequest request, CancellationToken cancellationToken, Func<Task<TResponse>> next, IMessageBus bus, string path, IDictionary<string, object> headers);
    }

    public interface IConsumerInterceptor<in TMessage>
    {
        Task OnHandle(TMessage message, CancellationToken cancellationToken, Func<Task> next, IMessageBus bus, string path, IReadOnlyDictionary<string, object> headers, object consumer);
    }

    public interface IRequestHandlerInterceptor<in TRequest, TResponse>
    {
        Task<TResponse> OnHandle(TRequest request, CancellationToken cancellationToken, Func<Task<TResponse>> next, IMessageBus bus, string path, IReadOnlyDictionary<string, object> headers, object handler);
    }

This could allow adding cross-cutting concerns like:

  • introducing message validation when publishing to the bus (.Publish() or .Send()) or consuming
  • introduce more advanced message logging (log, when a message is sent or before or after it, gets handled)
  • this could allow introducing of further plugins like for FluentValidations

Released.