Search code examples
c#.netmediatr

MediatR Multiple Pipeline Behaviors


Hello I already use this pattern of Mediator CQRS with Pipeline Behavior for a while, but just now I'm facing a issue where the generic implementation of TResponse and TRequest are not enough. So I'm trying to understand if having two separate pipelines for two very specific request is a bad practice or a bad idea.

So the main idea is, having a PipelineBehavior<RequestOne, ResponseOne> to do a specific logic for the requestOne, and another PipelineBehavior<RequestTwo, ResponseTwo> to do another logic that doesn't apply to request one.


Solution

  • I don't think its a bad idea at all, you can create an interface for mark all the requests you want to implement the PipelineX, and other for PipelineY.

    Ex:
    You have the RequestOne and RequestTwo classes that need to go through PipelineX, so you create and interface like IPipelineOne and mark the classes with it, then use reflection to process inside the pripeline middleware. The PipelineY will still run through these two classes and the rest of the program as well with no problems