DDD 中的聚合对象
我创建了一个名为 Question
的聚合类。其中包含对 Answer
、Category
、Feedback
对象的引用。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您所解释的方式,我假设每个问题都有 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
来自 DDD 网站:
当您需要直接访问实体时,即除了直接从持久性存储中获取实体之外,没有其他方便的方法来获取该实体时,可以使用存储库。相反,如果您认为该实体大多数时候可以通过遍历您手头已有的另一个对象轻松获得,那么就不需要存储库。这里的答案、类别和反馈似乎就是这种情况。
通常存储库仅用于聚合根,但也可能有例外。
我建议您在开始构建领域模型之前阅读 DDD 蓝皮书或一些教程,以对 DDD 构建块有基本的理解。
From the DDD web site :
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.