Using INotificationHandler for a base classes
See original GitHub issueI have the following class hiarchy
public abstract class UserHasLeftTripEvent : INotification
{
}
public class PassengerHasLeftTripEvent : UserHasLeftLiveTripEvent
{
}
public class DriverHasLeftTripEvent : UserHasLeftLiveTripEvent
{
}
And then a handler that handles these notifications
public class UserHasLeftTripEventHandler : INotificationHandler<UserHasLeftTripEvent>
{
public async Task Handle(UserHasLeftTripEvent message, CancellationToken cancellationToken)
{
}
}
However when I publish my events using the following:
public List<INotification> domainEvents = new List<INotification>()
{
new DriverHasLeftTripEvent(),
new PassengerHasLeftTripEvent()
}
await Task.WhenAll(domainEvents.Select(async (ev) => { await _mediator.Publish(ev); }));
My events never reach my event handler. I’m assuming that I would need to actually publish UserHasLeftTripEvents for my handler to work? Is there any way to configure MediatR to use the derived classes?
Issue Analytics
- State:
- Created 5 years ago
- Comments:18 (9 by maintainers)
Top Results From Across the Web
MediatR doesn't handle contravariant notifications
I went to source code of MediatR and it cannot resolve dependency INotificationHandler<SpecificAuditRequestBase> . But if instead of one handler ...
Read more >How in MediatR we can have events (Notifications) async ...
MediatR executes all notifications in sequence and in sync. ... 1- First we have to make our Mediator from the base class ......
Read more >Diagnosing and Fixing MediatR Container Issues
It starts with having a notification that's a base type for other events: public interface IIntegrationEvent : INotification { }.
Read more >All you need to know about introducing the MediatR library ...
MediatR Library applied: Join us to discover what your code might look like if you created a separate class for each method in...
Read more >The Mediator Pattern In C# .NET – Part 3 – MediatR Library
This is Part 3 of a series on using the Mediator Pattern in C# .NET. ... public class Notifier1 : INotificationHandler<NotificationMessage> { public...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi, I really would like to create a handler that handles a family of notifications. FOr example I want that all notifications that derive from a certain base class (which implements the INotification) to be forwarded to rabbitMQ or saved into a storage. Having to create a derived handler is not a good option because besides the actual unnecessary class it forces me to add this for every new type of notification.
@JoshLefebvre There give this a shot - not the best but allows you to share the logic.
that should do it for you
Just put all the logic on the abstract base class