C#C
C#12mo ago
BoomSonK

Reuse handlers with MediatR?

Hi,
I would like to know the best approach to share the same code in a codebase with CQRS and MediatR. Suggest a simple CRUD. You have a GetById, but also an Update and Delete. For the Update and Delete handlers I also have to retrieve the entity first, but they use the same logic as the GetById handler. Now I was wondering what the best approach is to do this with principles in mind, such as DRY, SRP and the fact that you don't want to have too many dependencies to keep it modular. In short, is it common to call the Update and Delete handler into the GetById handler, or re-implement the GetById logic in the Update and Delete handler, or should I create an additional service where all those handlers get this logic from. The last option seems best to me, I think.

I'd love to hear from you!
Was this page helpful?