Closed JaronrH closed 11 months ago
You can achieve the same thing using generic constraints:
public class MyNotificationHandler<TNotification> : INotificationHandler<TNotification>
where TNotification : IMyNotification
This could also be addressed at the registration side, but you'd have to provide more details OR use a better/more feature rich container.
Using better DI solved the issue - I'm closing this request now! (also, the implementation above had a bug where the same handler could appear multiple times)
I ran into a case where I needed to extend how DI does Notification Handler lookups. In my case, I wanted handlers based on an implemented interface to be used. i.e. If the Notification implemented IMyNotification (which is based on INotification), I wanted to have a handler for INotificationHandler which would handle any notification that implements IMyNotification.
In order to add my fix, I needed to make my own IMediator where the only change is that it is now using my implementation of NotificationHandlerWrapperImpl<>. So, my request would be to allow this to be changed via configuration or have DI inject it. I would rather not have to duplicate the entire Mediator implementation just to add logic that exists in another class!
As far as the changes to NotificationHandlerWrapperImpl<>, here is what I did: 1) Get all interfaces implemented by TNotification which are based on INotification. 2) Generate Generic Types for INotificationHandler<> with these interfaces. 3) Use DI get get all handlers that implement the handlers. 4) Create NotificationHandlerExecutor's which use reflection to call the appropriate handlers found in DI. 5) Concat these handlers to the default handler.