.. _refMiddlewares: ########### Middlewares ########### A middleware is a piece of logic that is executed between the mediator and the message handler. It has been designed to be very similar to Asp.Net Core middlewares. You can use middlewares to create any pipeline that you need for your queries and events. .. image:: /images/mediate_middleware_flow.png :align: center Each middleware can do the following things: - Choose to invoke the next element in the pipeline. - Do logic before and after calling the next element in the pipeline. .. note:: The middlewares are invoked in the same registration order. Query Middleware ================ A query middleware allows you to create a specific pipeline for a query. Middleware creation ------------------- To create a query middleware you have to implement the ``IQueryMiddleware`` generic interface. .. sourcecode:: csharp /// /// Interface to implement a middleware to process a query before it reaches it's handler. /// Query type /// Query response type /// public interface IQueryMiddleware where TQuery : IQuery { /// /// Invoke the middleware logic /// /// Query object /// /// Delegate that encapsulates a call to the next element in the pipeline /// Task Invoke(TQuery query, CancellationToken cancellationToken, NextMiddlewareDelegate next); } For example: .. sourcecode:: csharp public class SampleQueryLoggerMiddleware : IQueryMiddleware { private readonly ILogger _logger; public SampleQueryLoggerMiddleware(ILogger logger) { _logger = logger; } public async Task Invoke(MyQuery query, CancellationToken cancellationToken, NextMiddlewareDelegate next) { _logger.LogDebug("Query log: ", query); //invoke the next middleware in the pipeline return await next(); } } .. note:: The ``NextMiddlewareDelegate next`` parameter is a delegate that encapsulates the call to the next element in the pipeline. Query middleware pipeline ------------------------- The following diagram demonstrates how middlewares are invoked under the hood. .. image:: /images/query_middleware_flow.png :align: center .. important:: You have to invoke ``next`` to execute the next element in the pipeline. You can short circuit the pipeline simply don't invoking ``next``. Event Middleware ================ An event middleware allows you to create a specific pipeline for an event. Middleware creation ------------------- To create an event middleware you have to implement the ``IEventMiddleware`` generic interface. .. sourcecode:: csharp /// /// Interface to implement a middleware to process an event before it reaches it's handlers. /// Event type /// public interface IEventMiddleware where TEvent : IEvent { /// /// Invoke the middleware logic /// /// Event object /// /// Delegate that encapsulates a call to the next element in the pipeline /// Task Invoke(TEvent @event, CancellationToken cancellationToken, NextMiddlewareDelegate next); } For example: .. sourcecode:: csharp public class SampleEventLoggerMiddleware : IEventMiddleware { private readonly ILogger _logger; public SampleEventLoggerMiddleware(ILogger logger) { _logger = logger; } public async Task Invoke(MyEvent @event, CancellationToken cancellationToken, NextMiddlewareDelegate next) { _logger.LogDebug("Event log: ", @event); //invoke the next middleware in the pipeline await next(); } } .. note:: The ``NextMiddlewareDelegate next`` parameter is a delegate that encapsulates the call to the next element in the pipeline. Event middleware pipeline ------------------------- The following diagram demonstrates how middlewares are invoked under the hood. .. image:: /images/event_middleware_flow.png :align: center .. important:: You have to invoke ``next`` to execute the next element in the pipeline. You can short circuit the pipeline simply don't invoking ``next``.