gpt4 book ai didi

c# - DDD : Referencing MediatR interface from the domain project

转载 作者:太空狗 更新时间:2023-10-29 18:19:45 26 4
gpt4 key购买 nike

我刚刚开始使用 DDD。我正在将域事件放入 CQRS 应用程序中,但我正在执行一项基本任务:如何在域项目中使用 MediatR.INotification 标记接口(interface)而不创建对基础结构的域依赖性。

我的解决方案分为以下四个项目:

MyApp.Domain
- Domain events
- Aggregates
- Interfaces (IRepository, etc), etc.
MyApp.ApplicationServices
- Commands
- Command Handlers, etc.
MyApp.Infrastructure
- Repository
- Emailer, etc.
MyApp.Web
- Startup
- MediatR NuGet packages and DI here
- UI, etc.

我目前在 UI 项目中安装了 MediatR 和 MediatR .net Core DI 包,并使用 .AddMediatR() 将它们添加到 DI,命令为

services.AddMediatR(typeof(MyApp.AppServices.Commands.Command).Assembly);

它从 AppServices 项目扫描并注册命令处理程序。

当我想定义一个事件时,问题就来了。要让 MediatR 处理我的领域事件,它们需要用 MediatR.INotification 接口(interface)标记。

namespace ObApp.Domain.Events
{
public class NewUserAdded : INotification
{
...
}

在这种情况下标记我的事件以便 MediatR 使用它们的正确方法是什么?我可以为事件创建我自己的标记接口(interface),但 MediatR 无法识别那些没有某种方法自动将它们转换为 MediatR.INotification 的方法。

这只是使用多个项目的缺点吗?但是,即使我使用的是单个项目,如果我在域部分中使用 MediatR.INotification,我也会在域中放置一个“外部”接口(interface)。

当我的用户实体继承自 EF 的 IdentityUser 时,我遇到了同样的问题。在那种情况下,网络共识似乎是务实的,继续前进,让轻微的污染避免很多麻烦。这是另一个类似的案例吗?我不介意为了实用主义而牺牲纯洁性,但不仅仅是为了懒惰。

这是我使用的其他软件包会出现的一个基本问题,因此我期待解决这个问题。

谢谢!

最佳答案

您的域层最好不依赖于任何基础设施,但由于绑定(bind)的原因,这在 CQRS 中很难获得。我可以根据我的经验告诉你。但是,您可以最大限度地减少这种依赖性。一种方法是创建您自己的扩展 MediatR.INotificationEventInterface 并在整个域代码中使用该接口(interface)。这样,如果您想要更改基础架构,只需更改一个地方。

关于c# - DDD : Referencing MediatR interface from the domain project,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47292941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com