Mediatr: Call another handler in one handler

Created on 29 Apr 2019  路  3Comments  路  Source: jbogard/MediatR

Hi, I have an A handler it needs to call B and C handler to get specific data. I DI the IMediator in the A handler and call _mediator.Send(new C_Command()) and _mediator.Send(new B_Command()). There are specific API use B and C directly, so I wouldn't like to put B and C logic in A handler.

I'd like to know how to write a unit test for A handle because it needs to DI the IMediator. Or use mediator in the handler is a bad approach? If so, I need to write static functions Static_B() and Static_C() and call these function in the A, B, C handler.

I get the same situation when I need to publish a notification by _mediator.Publish() in the handler. I need to DI it but don't know how to write a unit test.

I love MediatR, I'd like to know the best clean architecture. Thanks a lot.

Most helpful comment

I try not to send requests from inside requests - it gets a bit messy, and it's not clear if the common behavior is a "top-level" request or not. Instead, treat that common behavior as something to refactor to a common class or method.

All 3 comments

I try not to send requests from inside requests - it gets a bit messy, and it's not clear if the common behavior is a "top-level" request or not. Instead, treat that common behavior as something to refactor to a common class or method.

@jbogard Appreciate your article!
I also find this article very useful.

Was this page helpful?
0 / 5 - 0 ratings