title: Replacing service layer with MediatR - is it worth to do it?
body: <p>Do you think it might be reasonable to replace my service layer or service classes with MediatR? For example, my service classes look like this:</p>
  
  <pre><code>public interface IEntityService&lt;TEntityDto&gt; where TEntityDto : class, IDto
  {
      Task&lt;TEntityDto&gt; CreateAsync(TEntityDto entityDto);
      Task&lt;bool&gt; DeleteAsync(int id);
      Task&lt;IEnumerable&lt;TEntityDto&gt;&gt; GetAllAsync(SieveModel sieveModel);
      Task&lt;TEntityDto&gt; GetByIdAsync(int id);
      Task&lt;TEntityDto&gt; UpdateAsync(int id, TEntityDto entityDto);
  }
  </code></pre>
  
  <p>I want to achieve some sort of modular design so other dynamically loaded modules 
   or plugins can write their own notification or command handlers for my main core application.</p>
  
  <p>Currently, my application is not event-driven at all and there's no easy way for my dynamically loaded plugins to communicate.</p>
  
  <p>I can either incorporate MediatR in my controllers removing service layer completely or use it with my service layer just publishing notifications so my plugins can handle them.</p>
  
  <p>Currently, my logic is mostly CRUD but there's a lot of custom logic going on before creating, updating, deleting.</p>
  
  <p>Possible replacement of my service would look like:</p>
  
  <pre><code>public class CommandHandler : IRequestHandler&lt;CreateCommand, Response&gt;, IRequestHandler&lt;UpdateCommand, Response&gt;, IRequestHandler&lt;DeleteCommand, bool&gt;
  {
      private readonly DbContext _dbContext;
  
      public CommandHandler(DbContext dbContext)
      {
          _dbContext = dbContext;
      }
  
      public Task&lt;Response&gt; Handle(CreateCommand request, CancellationToken cancellationToken)
      {
          //...
      }
  
      public Task&lt;Response&gt; Handle(UpdateCommand request, CancellationToken cancellationToken)
      {
          //...
      }
  
      public Task&lt;bool&gt; Handle(DeleteCommand request, CancellationToken cancellationToken)
      {
          ///...
      }
  }
  </code></pre>
  
  <p>Would it be something wrong to do?</p>
  
  <p>Basically, I'm struggling what to choose for my logic flow:</p>
  
  <ul>
  <li>Controller -> Service -> MediatR -> Notification handlers -> Repository</li>
  <li>Controller -> MediatR -> Command handlers -> Repository</li>
  </ul>
  
  <p>It seems like with MediatR I can't have a single model for Create, Update and Delete, so one way to re-use it I'd need to derive requests like:</p>
  
  <pre><code>public CreateRequest : MyDto, IRequest&lt;MyDto&gt; {}        
  public UpdateRequest : MyDto, IRequest&lt;MyDto&gt; {} 
  </code></pre>
  
  <p>or embed it in my command like:</p>
  
  <pre><code>public CreateRequest : IRequest&lt;MyDto&gt;
  {
      MyDto MyDto { get; set; }
  }
  </code></pre>
  
  <p>One advantage of MediatR is the ability to plug logic in and plug it out easily which seems like a nice fit for modular architecture but still, I'm a bit confused how to shape my architecture with it.</p>