Can one handler call another MediatR request? #1037
Replies: 2 comments
-
Hi @grosch-intl Technically, it is possible to have a "chain" of requests as you mentioned.
In general, I think that, as always in programming, the correct answer depends on how you implement the flow and what your fault tolerance requirements are. |
Beta Was this translation helpful? Give feedback.
-
Yes I've done this before.. however, looking back I wish I hadn't. The whole purpose of the mediator pattern is to segregate those actions. The whole point is to isolate each method with small namespaced classes that contain only the code needed to handle that one command / request / query / action, whatever. By making those handlers dependent upon other handlers you are introducing coupling. Basically, negating the benefits of MediatR. The best approach is to implement the same logic in the handler that is using it.. that way changes to one handler don't affect the other. Just my opinion. |
Beta Was this translation helpful? Give feedback.
-
I've seen things saying not to have one handler call another, and I'm not quite sure why. For example, lets say I have a command to generate the documents related to a checklist. Then I have multiple other commands to generate distinct checklist types. But all of them need those same documents. So within each GenerateChecklistTypeXyzHandler I inject IMediator and then the Handle method does something like
checklist.Documents = await mediator.Send(new GenerateDocuments(), cancellationToken);
and then sends the newly generated checklist as the return.Is there a better way to handle this?
Beta Was this translation helpful? Give feedback.
All reactions