title: Add a generic handler for Send and Publish methods of the MediatR library in asp .net core
body: <p>I use the CQS pattern in my asp.net core project. Let's start with an example to better explain what I want to achieve. I created a command:</p>
  
  <pre><code>public class EmptyCommand : INotification{}
  </code></pre>
  
  <p>The command handler:</p>
  
  <pre><code>public class EmptyCommandHandler : INotificationHandler&lt;EmptyCommand&gt;
  {
      public Task Handle(EmptyCommand notification, CancellationToken cancellationToken)
      {
          return Task.FromResult(string.Empty);
      }
  }
  </code></pre>
  
  <p>The query:</p>
  
  <pre><code>public class EmptyQuery : IRequest&lt;string&gt;{}
  </code></pre>
  
  <p>The query handler:</p>
  
  <pre><code>public class EmptyQueryHandler : IRequestHandler&lt;EmptyQuery, string&gt;
  {
      public Task&lt;string&gt; Handle(EmptyQuery notification, CancellationToken cancellationToken)
      {
          return Task.FromResult(string.Empty);
      }
  }
  </code></pre>
  
  <p>and this is a simple example of how to run the command and query and invoke the Handle method from the EmptyCommandHandler and EmptyQueryHandler:</p>
  
  <pre><code>readonly IMediator _mediator;
  
  public HomeController(IMediator mediator)
  {
      _mediator = mediator;
  }
  
  public async Task&lt;IActionResult&gt; Index()
  {
      await _mediator.Publish(new EmptyCommand());
      var queryResult = await _mediator.Send(new EmptyQuery());   
      return View();  
  }
  </code></pre>
  
  <p>Please bear in mind that query can return other types not necessarily the <code>string</code>.
  I would like to create some kind of a bridge class e.g. <code>MediatorBoostrapper</code>, which allows me to run some business logic(e.g. log command/query via Logger) every time the Publish method is invoked and then
  invoke the <code>public Task Handle(EmptyCommand notification,...</code> method from the command handler. The solution must be generic, so this method would be invoked every time I run the <code>Publish</code> method. I also want to be able to do the same thing for the <code>Send</code> method.</p>
  
  <p>I was thinking about the creation of the <code>public class MediatorBoostrapper : IMediator</code>
  but not sure what should be a proper implementation of the class and if my idea is good.
  Any ideas? Cheers</p>
  
  <p><strong>Edit</strong></p>
  
  <ol>
  <li><p>I want to have an example of how to use the <a href="https://github.com/jbogard/MediatR/wiki/Behaviors" rel="noreferrer">Behaviors</a>
  to create a generic way to run some external method from the generic handler every time I Run the <code>Send</code> method for queries. I want to have a similar example for <code>Publish</code> method, which I use for sending commands.</p></li>
  <li><p>I want to have an example of how to use <a href="https://github.com/jbogard/MediatR/wiki#polymorphic-dispatch" rel="noreferrer">Polymorphic dispatch</a>
  for the creation of the GenericCommandHandler and a GenericQueryHandler</p></li>
  </ol>
  
  <p>I created a sample project on GitHub which can be found <a href="https://github.com/demos666/mediatr-generic-handler" rel="noreferrer">here</a>
  You can feel free to try to extend this project with your solution.</p>