Sponsorship is available! If you’d like to sponsor CodeOpinion.com and have your product or service advertised exclusively (no AdSense) on every post, contact me.I was happy and surprised to see that MediatR v3.0 was released yesterday. One of the new features, which I was really looking forward to are pipeline behaviors.
A pipeline behavior is an implementation of
IPipelineBehavior<TRequest, TResponse>. It represents a similar pattern to filters in ASP.NET MVC/Web API or pipeline behaviors in NServiceBus.
ChangesThere are a couple major breaking changes to v3. All of them I’m fairly happy about actually.
MessagesThere is no distinction between sync, async (cancellable) requests or notifications. You simply implement either
INotification. You must still create the appropriate handler via either
AsyncTo send a request or publish a notification simply use
Task Publish(INotification)No longer does it have the “Async” convention of appending to the method name. I know this is often debated about this convention. I don’t mind it at all since there are no sync methods.
BehaviorsThe new addition is the interface
IPipelineBehavior<TRequest, TResponse>This allows you to create implementation(s) that will invoked in the order they are registered with your container (returned from the
MultiInstanceFactorydelegate). The simplest implementation, that does nothing but call the next possible behavior. You can see how you can use this to create Pre and Post behaviors to create a pipeline.
Pre & Post ProcessorsThankfully, these have already been created and built-in to v3.
<TRequest, TResponse>to the rescue. These can be used to implementing the appropriate interface of
IRequestPostProcessor<TRequest,TResponse>Note: Be sure to register your
IRequestPostProcessor<TRequest,TResponse>as well as
<TRequest, TResponse>with your container.