MediatR icon indicating copy to clipboard operation
MediatR copied to clipboard

INotificationHandler

Open kalyanthumaty opened this issue 2 years ago • 3 comments

I have read 1 notification can have N handlers, its a one to many relationship. In such case, what is the order that the handlers gets executed? Can we have a control to set the order of execution among handlers? Also, If I want to carry the output of one handler into another handler, or executing one handler based on the end result of another handler, is that possible?

kalyanthumaty avatar Jul 19 '22 06:07 kalyanthumaty

You should not use events/notifications this way. The order depends on the implementation of the di-container used, and it can change when new versions are released.

eskaufel avatar Jul 19 '22 07:07 eskaufel

What about managing execution order in NotificationHandlerWrapperImpl? We can create our own implementation of this, but we can't use it because we create implementation via Activator + it's private method image

KyMback avatar Jul 19 '22 08:07 KyMback

You should not use events/notifications this way. The order depends on the implementation of the di-container used, and it can change when new versions are released.

@eskaufel Thanks for your response but I did not quiet get you. Let me ask my questions in a different way by taking a use case below.

I have a customer who placed an order => OrderPlacedEvent : INotification

After the order is placed, I have to perform the following three actions

  1. Save the order details to DB => OrderPlacedSaveToDB : INotificationHandler<OrderPlacedEvent>
  2. Send email to customer => OrderPlacedEmailNotification : INotificationHandler<OrderPlacedEvent>
  3. Set the status to "OrderPlaced" => OrderPlacedSetStatus : INotificationHandler<OrderPlacedEvent>

Question #1: Can I use MediatR INotification like above. Question #2: If the answer is yes, then in what order are these handlers get invoked? Question #3: If the Handler#1 fails to do its job then the subsequent handlers should not be invoked. Is that possible?

Please respond. Thanks

kalyanthumaty avatar Aug 03 '22 14:08 kalyanthumaty