域对象是否应该注入依赖项? [英] Should domain objects have dependencies injected into them?

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

问题描述

我特别指的是这个问题: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?

推荐答案

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

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.

Factory 是一种隔离对象构造逻辑的模式.这也是 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天全站免登陆