首页 > 解决方案 > 如何将 MediatR 与我的业务层分离

问题描述

早上好。

我在我的项目中使用域事件,我发现实现它的最简单方法是使用 MediatR。但我不希望我的项目直接依赖它,我想应用依赖反转来隐藏库。

由于 INotification 接口,在 Mediator 中具有依赖关系的当前代码

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

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

但我想成为这样:

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

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

通过某种方式,我需要将中介事件/事件处理程序“转换”为我的项目事件/事件处理程序。

最好的方法是什么。提前致谢。

标签: .net-coremediatr

解决方案


我最终使用 StructureMap 和反射创建了我的域事件自定义代码,以在运行时解析事件处理程序。此处的代码示例:https ://github.com/Henry-Keys/domain-events-sample


推荐阅读