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.

Using INotificationHandler for a base classes

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

6reactions
bicatucommented, Nov 28, 2019

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.

3reactions
lilasquaredcommented, Sep 12, 2018

@JoshLefebvre There give this a shot - not the best but allows you to share the logic.

public abstract class UserHasLeftTripEventHandler<T> : INotificationHandler<T> where T : UserHasLeftTripEvent
{...}

public class PassengerHasLeftTripEventHandler : UserHasLeftTripEventHandler<PassengerHasLeftTripEvent>
{...}

public class DriverHasLeftTripEventHandler : UserHasLeftTripEventHandler<DriverHasLeftTripEvent>
{...}

that should do it for you

Just put all the logic on the abstract base class

Read more comments on GitHub >

github_iconTop 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 >

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 Hashnode Post

No results found