如何将 MediatR 与我的业务层分离 [英] How to decouple MediatR from my business layer
问题描述
早上好.
我在我的项目中使用域事件,我发现实现它的最简单方法是使用 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 接口,在 Mediator 中具有依赖关系的当前代码
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 和反射创建了域事件自定义代码,以在运行时解析事件处理程序.此处的代码示例:https://github.com/Henry-Keys/domain-events-样品
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 与我的业务层分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!