2

Good morning.

I'm using domain events in my project, and the easiest way i found to implement it was by using MediatR. But i don't want my project to directly depend on it, i want apply dependency inversion to hide the library.

Current code that has a dependency in Mediator, because of INotification interface

public class EmailConfirmedEvent : INotification
{
    public Guid PassengerId { get; }
    public string Email { get; }

    public EmailConfirmedEvent(Guid passengerId, string email)
    {
        Email = email;
        PassengerId = passengerId;
    }
}

But i want to be like this:

public class EmailConfirmedEvent : IMyProjectDomainEvent
{
    public Guid PassengerId { get; }
    public string Email { get; }

    public EmailConfirmedEvent(Guid passengerId, string email)
    {
        Email = email;
        PassengerId = passengerId;
    }
}

By some way i'll need to "convert" from mediator events/event handlers to my project events/event handlers.

What's the best way to do this. Thanks in advance.

  • You would end up having to create your own version of MediatR. which is a lot of effort. Is the trade off worth it? – Nkosi May 26 '18 at 13:13
  • 1
    Well, i'm considering a future that i'll end up switching libraries. I don't want to recreate an event dispatching library, i just want to abstract away MediatR from my code – Henrick Kakutalua May 26 '18 at 14:08

2 Answers2

4

I ended up creating my domain event custom code using StructureMap and reflection to resolve the event handlers at runtime. Code sample here: https://github.com/Henry-Keys/domain-events-sample

  • This helps to "abstract the abstraction" of Mediatr. I need to do the same thing using .NET Core's built in `IServiceProvider` with Lamar underneath. – flipdoubt Jul 12 '20 at 13:10
  • Your Github's link is broken. Could you update it ou place your code here? – Félix Severo May 14 '21 at 01:42
2

I generally make base classes that inherit from MediatR interfaces/base. Then if you change libraries (unlikely) you just have to update the base classes and the rest of the implement remains untouched.

Shane Ray
  • 1,449
  • 1
  • 13
  • 18