DDD 中的聚合对象

发布于 2025-01-05 17:34:52 字数 232 浏览 2 评论 0原文

我创建了一个名为 Question 的聚合类。其中包含对 AnswerCategoryFeedback 对象的引用。 QuestionRepository 是否应该包含查询与问题相关的数据库的所有方法以及查询反馈、答案等的所有方法?或者这些应该是单独的类,例如 QuestionRepository、FeedbackRepository 等。

I have made an aggregate class named Question. This contains references to Answer, Category, Feedback objects. Should the QuestionRepository be the class that contains all methods quering the database that relates to the Question but also all the methods for quering the Feedback, Answer etc? Or should these be seperate classes such as QuestionRepository, FeedbackRepository and so on.

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

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

发布评论

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

评论(2

远山浅 2025-01-12 17:34:52

根据您所解释的方式,我假设每个问题都有 1 个或多个答案、1 个或多个反馈,并且该问题属于特定类别,

因为答案和反馈依赖于问题并且不能独立存在,因此您可以有一个这 3 个实体的单个 QuestionRepository 。

说到类别,类别更多的是静态实体,IMO 是静态列表,因此所有此类静态实体都可以在 StaticRepository 中分组在一起

From the way you have explained , I am assuming that each Question will have 1 or more Answers , 1 or more Feedback and the Question belongs to a particular Category

Since the Answer and Feedback are dependent on Question and cannot exist independently , you can have a single QuestionRepository for these 3 entities .

Coming to Category , category is more of a static entity which IMO is a static list , so all such static entities can be grouped together in a StaticRepository

丿*梦醉红颜 2025-01-12 17:34:52

来自 DDD 网站

对于需要全局访问的每种类型的对象,创建一个对象
可以提供内存中所有集合的错觉
该类型的对象。

当您需要直接访问实体时,即除了直接从持久性存储中获取实体之外,没有其他方便的方法来获取该实体时,可以使用存储库。相反,如果您认为该实体大多数时候可以通过遍历您手头已有的另一个对象轻松获得,那么就不需要存储库。这里的答案、类别和反馈似乎就是这种情况。

通常存储库仅用于聚合根,但也可能有例外。

我建议您在开始构建领域模型之前阅读 DDD 蓝皮书或一些教程,以对 DDD 构建块有基本的理解。

From the DDD web site :

For each type of object that needs global access, create an object
that can provide the illusion of an in-memory collection of all
objects of that type.

A repository is used when you need direct access to an entity, i.e. when there's no other convenient way to get hold of that entity than fetching it from a persistent store directly. In contrast, if you consider that the entity is most of the time easily obtainable through traversal of another object you've already got at hand, then there's no need for a repository. It seems to be the case with Answer, Category, and Feedback here.

Usually repositories are only for aggregate roots, though there may be exceptions.

I suggest you read the DDD blue book or some tutorial to get a basic comprehension of the DDD building blocks before you start building your domain model.

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