16

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.

Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
Whoever
  • 1,295
  • 1
  • 14
  • 21
  • Are you talking about [this library](https://github.com/jbogard/MediatR)? – Cheng Chen Mar 01 '18 at 03:26
  • I have done this. It's ok, as long as you manage it. It's a quick way of getting in to a stack overflow exception if you're not careful, however. Keep the handlers small, and you'll be fine – Alex Mar 01 '18 at 11:24
  • @DannyChen Yes. – Whoever Mar 01 '18 at 12:57
  • @Alex Thanks. Overflow, you mean getting into circular calling? Now that I looked at it again, some of those sub calls are mostly internal and do not have a direct "request" from end user. Maybe writing them as "service" instead? – Whoever Mar 01 '18 at 12:59
  • yeah, exactly that. `AbcHandler` calls `XyzHandler` which in turn calls `AbcHandler` – Alex Mar 01 '18 at 14:08
  • it's hard to say without seeing your code, but I've found there's very little that can't be architected into MediatR commands / handlers - especially with notifications too – Alex Mar 01 '18 at 14:09
  • Thanks. I'll just make a mental note that "service" should not call handler. – Whoever Mar 01 '18 at 14:50

1 Answers1

9

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 :-)

anielsen
  • 95
  • 1
  • 7
  • 2
    While this may answer the question, consider the fact that links rot away. Therefore please consider summarising the content you link to. – warped Dec 09 '19 at 07:51