How to decouple MediatR from my business layer

懵懂的女人 提交于 2020-01-06 05:50:15

问题


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.


回答1:


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




回答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.



来源:https://stackoverflow.com/questions/50539986/how-to-decouple-mediatr-from-my-business-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!