通过域封装持久性,还是通过存储库持久性?
如果我的域模型不应该知道/关心存储库,那么像 .UpdateOrder(...)
这样封装 CRUD 更新的行为如何与存储库交互?通过域服务?
好的,那么我的存储库有一个有效的 CRUD 更新,它与我的 .UpdateOrder(...)
结合使用。没关系。但我不希望有人在存储库上使用 Update 方法,我希望他们检查实体上的行为(改为使用 UpdateOrder() )。我更喜欢类似于我的域模型满足不变量的方式 - 通过它的设计(私有集属性等) - 我的存储库不公开“更新”/持久化实体的替代方法。
这只是一个访问修饰符问题吗?我在 Repo public 中没有 Update 方法就解决了这个问题。或者有一个“更好”的答案吗?请 DDD 忍者帮助我。
If my Domain Model is not supposed to know/care about the Repository, then how does some behaviour like .UpdateOrder(...)
, that encapsulates a CRUD-Update, interface with the Repository? Through a Domain Service?
Ok, then my Repository has an effective CRUD-Update that's used in conjunction with my .UpdateOrder(...)
. That's fine. But i don't want someone to use the Update method on the Repository, i want them to go through the behaviour on the Entity (use UpdateOrder() instead). I'd prefer that in likeness to the way my Domain Model satisfies invariants - by it's design (private set properties, etc) - my Repository not expose an alternate method to "updating"/persisting the Entity.
Is this simply a access modifier problem that is solved by me not having the Update method in the Repo public. Or is there a 'better' answer? Please help me DDD ninjas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DDD 中的严格顺序是:
存储库始终负责检测/保留实体内的所有更改。
(顺便说一句,我假设你的实体是一个聚合根)
The strict sequence in DDD would be:
The repository is always responsible for detecting/persisting all of the changes within the entity.
(btw, I'm assuming that your entity is an aggregate root)
如果您的领域模型不包含持久性,那么它就不包含存储某些内容的操作。如果您的实体是来自领域模型的东西,那么它就没有必要持久化自己。
你说:
但我认为这是错误的。您的域对象没有比打印自身、在屏幕上绘制自身等更多的责任来保留自身。您的域类不应该有 UpdateOrder 方法。
现在,您可能不想将原始存储库(从持久性实现层)公开给其他代码,但这只是意味着将其包装在合适的东西中。听起来您确实有需要讨论持久性的代码,因此请弄清楚它需要在什么级别的讨论上工作,并为其公开一个合适的接口。
If your domain model doesn't include persistence, then it doesn't include the operation of storing something. If your entity is something from the domain model, then it has no business persisting itself.
You say:
But i think that's mistaken. Your domain objects have no more responsibility for persisting themselves than they do printing themselves, drawing themselves on screen, etc. Your domain class should not have a UpdateOrder method.
Now, you might not want to expose the raw repository (from your persistence implementation layer) to other code, but that just means wrapping it in something suitable. It sounds like you do have code that needs to talk about persistence, so figure out what level of discourse it needs to work at, and expose a suitable interface to it.