本文介绍了如何将MediatR与我的业务层脱钩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好。

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

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.

当前代码中存在依赖项介体,因为具有INotification界面

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;
    }
}

但我想这样:

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.

推荐答案

我最终使用StructureMap和反射来创建域事件自定义代码,并通过反射来解决事件处理程序在运行时。
代码示例在这里:

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

这篇关于如何将MediatR与我的业务层脱钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 17:51