是否可以在没有依赖项注入(DDD)的情况下在聚合(域层)中实现MediatR? [英] Is it possible to implement MediatR in the Aggregates (Domain Layer) without dependency injection (DDD)?

查看:231
本文介绍了是否可以在没有依赖项注入(DDD)的情况下在聚合(域层)中实现MediatR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为防止重新发明轮子,我想在聚合中使用MediatR发布域事件。 (Un)Fortunately(?)MediatR用作注入到类中的依赖项,而不是我可以静态调用的依赖项。因此,我最终将通过构造函数创建对库的直接依赖关系。

To prevent reinventing the wheel, I'd like to use MediatR in the Aggregates to publish domain events. (Un)Fortunately(?) MediatR works as a dependency that is injected into the classes, and not something that I can call statically. Therefore I'd end up creating a direct dependency on the library via the constructor.

我不记得在哪里读(如果我没看错的话),我应该避免在聚合的构造函数中使用非业务依赖项。

I don't remember where I read it (and if I read it right), that I should avoid non-business dependencies in the constructors of the Aggregates.

因此,我不应该这样做:

Therefore, I shouldn't do something like this:

public class MyAggregate
{
    private readonly IMediator _mediator;
    public MyAggregate(IMediator mediator)
    {
        _mediator = mediator;
    }
}

这让我深入思考是否有可能或建议(或不建议使用)在汇总中使用MediatR ...

That made me think deeply whether it was possible or recommended (Or not recommended) to use MediatR in the Aggregates...

我是否可以静态使用MediatR,还是应该实现自己的Event Dispatcher?

Is there a way for me to use MediatR statically or should I implement my own Event Dispatcher?

P.S:另外,如果我对聚合依赖项的理解不正确,请随时纠正我。

P.S: Also, feel free to correct me if my understanding of the Aggregates dependencies are wrong.

P.S x2:
我已经搜索过Google和SO,因此找不到答案。
https://stackoverflow.com/search?q=mediatr+domain+events
如何将MediatR与我的业务层分离
DDD:从域项目中引用MediatR接口

P.S x2: I've searched Google and SO and can't find an answer to this. https://stackoverflow.com/search?q=mediatr+domain+events How to decouple MediatR from my business layer DDD: Referencing MediatR interface from the domain project

推荐答案


我应该避免$ b的构造函数中的非业务依赖性$ b聚合。

I should avoid non-business dependencies in the constructors of the Aggregates.

不仅在构造函数中;您的业​​务层不应以任何形式依赖于非业务;甚至是静态的。

Not only in constructors; your business layer should not have dependencies to non-business in any form; even static.

我要做的只是将域事件从聚合返回到应用程序层,然后发布域事件。

What I do is just return the domain event from the aggregate to the application layer and then publish the domain event.

请阅读以下几篇文章,以更好地理解我的意思:

Please read these couple of post to better understand what I mean:

不要发布域事件,请返回它们!
DDD-Application-Services-Explained

这篇关于是否可以在没有依赖项注入(DDD)的情况下在聚合(域层)中实现MediatR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆