Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to have one handler call another when using MediatR?

Tags:

c#

oop

cqrs

mediatr

Or is that considered bad practice or something?

I have one notification triggers 4-5 handlers, which in turn call database to retrieve data. Each those calls can also be called separately, so they are request/handler themselves.

Thanks.

like image 953
Whoever Avatar asked Mar 01 '18 03:03

Whoever


People also ask

Is MediatR an anti pattern?

This is why MediatR became so popular. Developers love magic. Again, this is not a mediator pattern, but a service locator, an anti-pattern.

What is MediatR .NET core?

MediatR Requests are very simple request-response style messages, where a single request is synchronously handled by a single handler (synchronous from the request point of view, not C# internal async/await). Good use cases here would be returning something from a database or updating a database.


1 Answers

Jimmy Bogard (the author of mediatr) is saying you should avoid this.

See his blog post

https://lostechies.com/jimmybogard/2016/12/12/dealing-with-duplication-in-mediatr-handlers/

or this stack overflow question and answer https://github.com/jbogard/MediatR/issues/400

Also note that if you are using behaviors they will run multiple times. This could lead to inefficient code but of course it could also be what you want to happen :-)

like image 173
anielsen Avatar answered Oct 24 '22 01:10

anielsen