question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Call another handler in one handler

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
jbogardcommented, Apr 29, 2019

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.

3reactions
OculiViridicommented, Jun 10, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Is it OK to have one handler call another when using ...
I have one notification triggers 4-5 handlers, which in turn call database to retrieve data. Each those calls can also be called separately, ......
Read more >
Calling one handler from another one, is it good? #434
So my question is that can I call that query handler bypassing user id with it and then get that user and then...
Read more >
Why You Should Avoid Command Handlers Calling Other ...
The easiest way to implement this, one might think, is to add in two more commands - SendShippingNotificationCommand and SendOrderEmailCommand ...
Read more >
Chaining Handlers with MediatR : r/csharp
Each 'handler' should have just been a request filter or normal service class call. The fact that you want one handler to call...
Read more >
Calling an Handler from another one
Hi, I'd like to instantiate and call another handler from the current handler using the same method, using it to add to (or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found