DDD 是否允许列表作为聚合根?
我试图了解领域驱动设计的基础知识。昨天,我在我正在使用的一个项目中发现了一些代码,其中存储库返回了实体列表,即 List getMessages(),其中 Message 是一个实体(有自己的 id 并且可以修改)。现在,当阅读 DDD 中的存储库时,他们非常明确地指出存储库应返回聚合根,并且聚合上的任何操作都应通过调用聚合根中的方法来完成。
我想将列表放在它自己的类中,然后返回该类。但是,在我的项目中,除了遵守 DDD 之外,基本上不需要这样做,因为我们只显示消息、添加新消息或删除现有消息。我们永远不需要删除所有消息,因此我们唯一拥有的方法是,addMessage(...)
、getMessages()
、updateMessage(.. .)
和 removeMessage(...)
这基本上就是我们的域服务正在做的事情。
有人有什么想法吗?在描述聚合和存储库时,DDD 的最佳实践是什么?
I am trying to understand the fundamentals of Domain-driven design. Yesterday I found some code in a project I am working with where a Repository returned a list of Entities, i.e. List getMessages() where Message is an entity (has its own id and is modifiable). Now, when reading about Repositories in DDD they are pretty specific that the Repository should return the Aggregate Root, and that any actions on the aggregate should be done by invoking methods in the Aggregate Root.
I would like to place the List in its own class and then just return that class. But, in my project there is basically no need to do that except for compliance with DDD, since we only show messages, add new ones or remove an existing message. We will never have to remove all messages, so the only methods we would have are, addMessage(...)
, getMessages()
, updateMessage(...)
and removeMessage(...)
which is basically what our Domain Service is doing.
Any ideas anyone? What is the best practice in DDD when it comes to describe Aggregates and Repositories?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 DDD 新手来说,令人困惑的方面之一是存储库概念。
存储库:
使用类似集合的接口来访问域对象,在域和数据映射层之间进行中介。
存储库提供了获取对聚合根的引用的能力。不是实体,值对象,而是聚合根(我不同意“存储库应该返回聚合根”)。
建议:
- 每个聚合根一个存储库
希望这会有所帮助!
One of the confusing aspects of those new to DDD is repository concept.
Repository:
Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.
A Repository provides the ability to obtain a reference to an Aggregate root. Not Entity, Value Object, but Aggregate root ( i dont agree with "Repository should return the Aggregate Root").
Suggestions:
- One repository per aggregate root
Hope this help!!