title: Is MediatR library overused in CQRS examples on the web?
body: <p>I'm struggling to understand why so many examples on the web are using MediatR when explaining CQRS patterns, when dealing with commands and queries.</p>
  <p>Almost everywhere I see examples where Commands and Queries are handled by MediatR but I do not see any benefit from it other than not needing to register every Command or Query in Dependency Injection Container. But then you need to implement Query objects (inheriting IRequest), Query Handlers, and Query Responses objects so that in your API controller method you can then call <code>_mediatr.Send(queryObject)</code>.</p>
  <p>Why not just use dependency injection to inject query object into API controller which you can call &quot;get&quot; methods directly on? Like:</p>
  <pre><code>[HttpGet]
  [Route(&quot;getall&quot;)]
  public async Task&lt;IncidentQueryResult&gt; GetAll(int page = 0, int pageSize = 25)
  {
      var result = await _incidentQueries.GetIncidents(page, pageSize);
      return result;
  }
  </code></pre>
  <p>instead of:</p>
  <pre><code>[HttpGet]
  [Route(&quot;getall&quot;)]
  public async Task&lt;IncidentQueryResult&gt; GetAll(int page = 0, int pageSize = 25)
  {
      var query = new IncidentQuery(page, pageSize);
      var result = await _mediatr.Send(query);
      return result;
  }
  </code></pre>
  <p>Then, inside the <code>GetIncidents</code> method, there's a direct sql call to database and mapping results to C# objects. Plain and simple.</p>
  <p><strong>For me, the perfect and only reasonable use of MediatR library is to handle Domain Events.</strong>
  When implementing DDD, I'm trying to set up a project in the way presented below. Each rectangle is a different project in the solution. The arrows represents references:</p>
  <p><a href="https://i.stack.imgur.com/y30pb.png" rel="noreferrer"><img src="https://i.stack.imgur.com/y30pb.png" alt="DDD with CQRS schema" /></a></p>
  <p>Let's imagine a scenario: Creating a domain object needs to increment a counter stored in another domain object (a different aggregate)</p>
  <ol>
  <li>A request is made to API endpoint to add some new domain object to database <em>(layer 6: Presentation)</em></li>
  <li>The controller method uses a command injected in it's constructor to create a domain object <em>(layer 4: Commands)</em></li>
  <li>Inside the command, a new domain object is created along with &quot;domain object created&quot; event stored in this object, ready to be broadcasted just before saving to database</li>
  <li>Then the Command uses Repository from Infrastructure layer to add this newly created object to database.</li>
  <li>Then just before database save is performed: the &quot;domain object created&quot; event is sent through MediatR <em>(layer 2: Infrastructure)</em></li>
  <li>The event is then caught in layer <em>3: Application</em> in one of the Domain Events Handler.</li>
  <li>The domain event handler <em>(layer 3: Application)</em> uses Repository from Infrastructure layer to get another domain Aggregate holding a counter to be incremented and then increments the counter.</li>
  <li>All domain events were handled, save to database is performed.</li>
  </ol>
  <p>So the MediatR for me works only in <em>Infrastructure</em> and <em>Application</em> layer.</p>
  <p>Are people just using MediatR for Commands and Queries just for sake of using it? For me it looks like adding commands and queries handlers, query and command request and response types only adds more code that has no real value and only makes it less understandable.</p>
  <p>Here are some links I visited:</p>
  <ul>
  <li><a href="https://referbruv.com/blog/posts/implementing-cqrs-using-mediator-in-aspnet-core-explained" rel="noreferrer">https://referbruv.com/blog/posts/implementing-cqrs-using-mediator-in-aspnet-core-explained</a></li>
  <li><a href="https://www.edument.se/en/blog/post/net-5-source-generators-mediatr-cqrs" rel="noreferrer">https://www.edument.se/en/blog/post/net-5-source-generators-mediatr-cqrs</a></li>
  <li><a href="https://itnext.io/why-and-how-i-implemented-cqrs-and-mediator-patterns-in-a-microservice-b07034592b6d" rel="noreferrer">https://itnext.io/why-and-how-i-implemented-cqrs-and-mediator-patterns-in-a-microservice-b07034592b6d</a></li>
  <li><a href="https://www.rubicon-world.com/blog/2020/06/a-developers-guide-to-cqrs-using-net-core-and-mediatr/" rel="noreferrer">https://www.rubicon-world.com/blog/2020/06/a-developers-guide-to-cqrs-using-net-core-and-mediatr/</a></li>
  <li><a href="https://dotnetdetail.net/cqrs-and-mediator-patterns-in-asp-net-core-3-1/" rel="noreferrer">https://dotnetdetail.net/cqrs-and-mediator-patterns-in-asp-net-core-3-1/</a></li>
  </ul>
  <p>A lot of sites in my native language has this as well.</p>
  <p>Having too many handlers used throughout your application makes difficult to read what your application does and what triggers what. What I saw was that people were handling domain events in Commands layer but the domain probably shouldn't send commands directly?</p>
  <p>Do you need to use MediatR at all in CQRS?</p>