java - DDD 中的聚合对象

标签 java jakarta-ee domain-driven-design

我可以使用与其他类中的成员相同的聚合类吗? 如果是的话,包含聚合的类会强制执行访问等吗?

假设您有一个 User 类。然后是一个名为 LogBook 的类,最后是一个名为 Log/Post 的类(在那个巷子里的东西)。在我的示例中,LogBook 将是 Log/Post 类的聚合根,而 User 将是整体聚合。现在,User 类是否包含添加日志帖子等的方法?您可以在 User 类中创建一个方法来调用 LogBook 类,该类有一个方法可以执行实际添加日志的所有逻辑。

或者,聚合总是位于层次结构的顶部吗?没有嵌套。

最佳答案

这是一个不错的 definition聚合的:

Definition: A cluster of associated objects that are treated as a unit for the purpose of data changes. External references are restricted to one member of the Aggregate, designated as the root. A set of consistency rules applies within the Aggregate's boundaries. Problem: It is difficult to guarantee the consistency of changes to objects in a model with complex associations. Invariants need to be maintained that apply to closely related groups of objects, not just discrete objects. Yet cautious locking schemes cause multiple users to interfere pointlessly with each other and make a system unusable. [DDD, p. 126] Solution: Cluster the Entities and Value Objects into Aggregates and define boundaries around each. Choose one Entity to be the root of each Aggregate, and control all access to the objects inside the boundary through the root. Allow external objects to hold references to root only. Transient references to the internal members can be passed out for use within a single operation only. Because the root controls access, it cannot be blindsided by changes to the internals. This arrangemens makes it practical to enforce all invariants for objects in the Aggregate and for the Aggregate as a whole in any state change. [DDD, p. 129]

我认为您不希望 User 类在不经过 LogBook 类的情况下访问 LogBook 的聚合对象。但是,从用户访问日志似乎没问题。

关于java - DDD 中的聚合对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9330890/

相关文章:

java - 将 SharedPrefs 编辑器放入实用程序类?

java - Axis 2+壁垒 : Must Understand check failed for header Security

java - 观察者模式建议

spring-mvc - Spring 和 MongoDB : Saving Value Objects in a more flat way

java - 单一职责原则与贫/富域模型有何关系?

java - 当 str 是点 ("."时,如何让 Pattern.matches(regex, str) 返回 false ?

Java Apache Log4j log4j.xml 配置多个错误

java - Struts2:URL Action 相对路径

java - 如何访问 jpql 从托管 bean 中的 ejb 返回的集合

domain-driven-design - 在 DDD/CQRS 中,ReadModel 是否应该充当 ViewModel,如果不是,那么映射的责任在哪里?