在 DDD 中使用聚合

发布于 2024-09-27 17:07:35 字数 169 浏览 1 评论 0原文

寻找有关使用聚合根的一些说明。

如果我有一个模型(试卷)如下;

试卷 --->问题--->答案

和我已经确定问题论文是一个聚合根,如果我想选择一个问题的答案,我是否必须在聚合根上放置一个公共方法,或者我可以从根公开问题并放置一个公共方法吗?在问题对象上选择答案??

Looking for some clarification on working with aggregate roots.

If I have a model (a question paper) as follows;

QUESTION PAPER ---> QUESTION ---> ANSWER

and I have identified that the QUESTION PAPER is an aggregate root, if I want to select a answer for a question do I have to put a public method on the aggregate root or can I expose the questions from the root and put a public method on the QUESTION object to select a ANSWER??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一曲琵琶半遮面シ 2024-10-04 17:07:35

一般来说,你总是想与你的聚合根交谈。如果您正在读取值,那么有时将公共访问器添加到聚合根内的聚合可能会很方便,但它很快就会变得丑陋(德米特法则,破坏抽象等),我建议您不要这样做做吧。

然而,对于任何改变状态的事情,始终遍历聚合根至关重要。聚合根代表一致性边界(即,它直接或间接地负责将事物保持在有效状态),如果您允许状态更改,您就完全绕过了这一点,从而为不断增加的复杂性打开了大门。

所以,这取决于你的问题中“选择”的含义 - 如果你正在查询,那么你可以逃脱它,但这是一个坏主意。如果您要更改状态,请不要这样做,否则您的聚合根不再是聚合根。

In general you always want to be talking to your aggregate root. If you're reading values then sometimes it can be convenient to add public accessors to aggregates inside the aggregate root, but it gets ugly (Law of Demeter, breaking abstractions, etc, etc) very quickly and I would suggest that you don't do it.

For anything that changes state, however, it's critical that you always go through the aggregate root. The aggregate root represents a consistency boundary (i.e. it is responsible, either directly or indirectly, for keeping things in a valid state) and if you allow state changes you bypass this altogether, opening the door to ever increasing complexity.

So, it depends what you mean by 'select' in your question - if you're querying then you can get away with it, but it's a bad idea. If you are changing state then don't do it, or your aggregate root is no longer an aggregate root.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文