title: Mediatr - Pre/post-processor specialization
body: <p>I'm currently revamping some large controllers in our asp.net core app. To do so we chose Mediatr and we are currently splitting those big actions into handlers &amp; pre/post processors.</p>
  
  <p>Some of our commands need to trigger an internal notification system (node.js service). To do so, I have developed a post processor in charge of notifying the event service. However, I want to "trigger" it <strong>only for commands inheriting from the <code>INotify</code> interface</strong>. In other words, Mediatr loads all pre/post processors but it triggers only those whose command type is matching the generic constraint. In the end it would look like this:</p>
  
  <pre><code> public class NotificationPostProcessor&lt;TCommand, TResponse&gt; : IRequestPostProcessor&lt;TCommand, TResponse&gt;
          where TCommand : &gt;&gt;INotifyCommand&lt;&lt;
          where TResponse : CommandResult
  {
       (...)
  }
  </code></pre>
  
  <p>If the command does not inherit from INotifyCommand then this post-processor is not triggered.</p>
  
  <p>Same thing goes for pre-processors. For example, I would need my pre-processor to add some extra data to some specific command. </p>
  
  <p>Currently what I do is <em>horrible</em> and I'm sure there is a better way.</p>
  
  <pre><code>public class NotificationPostProcessor&lt;TCommand, TResponse&gt; : IRequestPostProcessor&lt;TCommand, TResponse&gt;
      where TCommand : IRequest&lt;TResponse&gt;
      where TResponse : CommandResult
  {
      private readonly INotificationService _service;
  
      public NotificationPostProcessor(INotificationService service)
      {
          _service = service;
      }
  
      public async Task Process(TCommand command, TResponse response)
      {
          var cmd = command as NotifyBaseCommand;
          if (cmd != null &amp;&amp; response.IsSuccess)
              await _service.Notify(cmd.Event, command, response);
      }
  }
  </code></pre>
  
  <p>Since I'm using the default asp.net core dependency injection engine + <code>MediatR.Extensions.Microsoft.DependencyInjection</code> package, I'm not directly registering Post &amp; Pre processors. </p>
  
  <pre><code>   // Pipeline engine used internally to simplify controllers
      services.AddMediatR();
      // Registers behaviors
      services.AddTransient(typeof(IPipelineBehavior&lt;,&gt;), typeof(Pipeline&lt;,&gt;));
      services.AddTransient(typeof(IPipelineBehavior&lt;,&gt;), typeof(AuditBehavior&lt;,&gt;));
      services.AddTransient(typeof(IPipelineBehavior&lt;,&gt;), typeof(RequestPreProcessorBehavior&lt;,&gt;));
      services.AddTransient(typeof(IPipelineBehavior&lt;,&gt;), typeof(ValidationBehavior&lt;,&gt;));
      services.AddTransient(typeof(IPipelineBehavior&lt;,&gt;), typeof(RequestPostProcessorBehavior&lt;,&gt;));
      // Registers command validator
      services.AddTransient(typeof(IValidator&lt;RegisterUserCommand&gt;), typeof(RegisterUserCommandValidator));
  </code></pre>
  
  <p>I'm must admit I'm a little bit lost here. Any idea on how i can improve this system?</p>
  
  <p>Thank you,
  Sebastien</p>