域对象是否注入了依赖关系? [英] Should domain objects have dependencies injected into them?

查看:148
本文介绍了域对象是否注入了依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具体提到这个问题:
DDD - 如何实施工厂

I'm specifically referring to this question: DDD - How to implement factories

所选答案表示:

工厂不应与依赖关系注入绑定,因为域对象不应该将注入到它们的依赖关系。

"factories should not be tied with dependency injection because domain objects shouldn't have dependencies injected into them."

我的问题是:无法将依赖关系注入您的实体的原因是什么?还是我只是误会了这个声明?有人可以澄清吗?

My question is: what is the reasoning of not being able to inject dependencies in to your entities? Or am I just misunderstanding the statement? Can someone please clarify?

推荐答案

域对象不是工厂,Repos等,它们只是实体,价值对象,域服务和总体根。也就是说,它们必须是封装您的业务域使用的数据,它们之间的关系以及域可以对该数据执行的行为(读取修改)的类。

Domain Objects aren't Factories, Repos, etc. They are only Entities, Value Objects, Domain Services and Aggregate Roots. That is, they must be classes which encapsulates the data your business domain uses, the relationships between them, and the behaviour(read modifications) that the domain can do on that data.

Repository是一种抽象化您正在使用的持久性基础架构的模式。它在DDD中,因为它使您的应用程序与数据库分离,但并不是所有的DDD应用程序都需要或甚至应该使用存储库。

Repository is a pattern to abstract away the persistence infrastructure you are using. It's in DDD because it makes your app decoupled from your database, but not all DDD app need or even should use repository.

工厂是一种隔离构建逻辑的模式对象。这也是DDD推荐的一个很好的做法,但在所有情况下都不是真正需要的。

Factory is a pattern to isolate the construction logic of objects. It's also just a good practice that DDD recommends, but not really needed in all scenarios.

域对象不应该依赖于其他任何东西,因为它们是您的核心应用程序。一切都将依赖于他们。所以保持他们没有其他的依赖关系使得一个明确的单向依赖链,并减少依赖图。它们是不变量,模型,基础。改变它们,你可能需要改变很多东西。所以改变别的东西不应该强迫他们改变。

Domain Objects shouldn't depend on anything else, because they are the core of your app. Everything will depend on them. So keeping them free of other dependency makes a clear one way dependency chain, and reduces the dependency graph. They are the invariants, the model, the foundation. Change them, and you probably need to change a lot of stuff. So changing other things shouldn't force them to change.

这篇关于域对象是否注入了依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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