本文介绍了DDD:从域项目引用MediatR接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用DDD.我正在将域事件放入CQRS应用程序中,并且绊脚石是一项基本任务:如何在域项目中使用MediatR.INotification标记接口,而又不创建对基础结构的域依赖性.

I'm just getting started with DDD. I'm putting domain events into a CQRS application and I'm stumbling on a fundamental task: How to use the MediatR.INotification marker interface within the domain project without creating a domain dependency on infrastructure.

我的解决方案由四个项目组成,如下所示:

My solution is organized in four projects as follows:

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中

I currently have the MediatR and MediatR .net Core DI packages installed in the UI project and they are added to DI using .AddMediatR(), with the command

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

从AppServices项目中扫描并注册命令处理程序.

which scans and registers command handlers from the AppServices project.

当我想定义一个事件时,问题就来了.为了使MediatR能够处理我的域事件,需要使用MediatR.INotification界面对其进行标记.

The problem comes when I want to define an event. For MediatR to work with my domain events, they need to be marked with the MediatR.INotification interface.

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

在这种情况下标记事件的正确方法是什么,以便MediatR可以使用它们?我可以为事件创建自己的标记界面,但是MediatR在没有某种自动将其转换为MediatR.INotification的方式时将无法识别它们.

What is the proper way to mark my events in this situation so they can be used by MediatR? I can create my own marker interface for events, but MediatR won't recognize those without some way to automatically cast them to MediatR.INotification.

这仅仅是使用多个项目的缺点吗?即使我只使用一个项目,但是如果我在domain部分中使用MediatR.INotification,我将在域中放置一个外部"接口.

Is this just a shortcoming of using multiple projects? Even if I was using a single project, though, I would be putting an "external" interface in the domain if I used MediatR.INotification from within the domain section.

当我的用户实体从EF的IdentityUser继承时,我遇到了同样的问题.在那种情况下,网上共识似乎是务实的,请继续进行下去,并允许较小的污染省去很多麻烦.这是另一个类似的情况吗?我不介意为实用主义而牺牲纯洁,而不仅仅是为了偷懒.

I've run into the same issue when my User entity inherited from EF's IdentityUser. In that case the web consensus seems to say be pragmatic and go ahead and allow the minor pollution to save a lot of headaches. Is this another similar case? I don't mind sacrificing purity for pragmatism, but not just to be lazy.

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

This is a fundamental issue that will occur with other packages I use, so I look forward to solving this.

谢谢!

推荐答案

最好,您的域层不依赖于任何基础架构,但是由于绑定,这在CQRS中很难获得.我可以根据我的经验告诉你.但是,您可以最大程度地减少这种依赖性.一种方法是制作自己的扩展了MediatR.INotificationEventInterface并在域代码中使用该接口.这样,如果您想更改基础架构,则只需在一个地方进行更改.

It is best that your domain layer doesn't depend on any infrastructure but that is hard to obtain in CQRS because of the bindings. I can tell you from my experience. You can, however, minimize that dependency. One way to do that is to make your own EventInterface that extends MediatR.INotification and use that interface all over the domain code. In this way, if you ever want to change the infrastructure, you need to change only in one place.

这篇关于DDD:从域项目引用MediatR接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 17:51