聚合和聚合根是否作为单独的 类 实现?

Are aggregates and aggregate roots implemented as separate classes?

关于 DDD 的讨论如此之多,我希望找到大量代码示例如何实现这些模式。但就没那么幸运了。

让我困惑的是这个。是一种聚合实体,表示为聚合根,还是作为单独的 classes 实现的实体?以下是几个例子,请回答是否正确。

  1. 它们是分开的 classes
class Aggregate {
   private Entity aggregateRootEntity;
   //methods that keep invariants
}
  1. 没有单独的聚合class,而是聚合根class,代表聚合
class AggregateRootEntity {
   //id, fields, value objects references, other entities references
   //methods that keep invariants
}
  1. 任何实体都可以是聚合,如果它在事务范围内使用,保持不变量,并“聚合”一些值对象
//This is aggregate:
class Entity {
   private List<ValueObject> valueObjects;
   //id, fields
   //methods that keep invariants
}
   

There is no separate aggregate class, but aggregate root class, which represents the aggregate

这个。

介绍 DDD 和“聚合”模式的参考书是 Eric Evans Domain Driven Design: Tackling Complexity int the Heart of Software

第 5 章讨论了在软件领域建模中使用的模式:实体、值、“领域服务”等。

第 6 章讨论了生命周期管理,其中他谈到了工厂、存储库和聚合:

An AGGREGATE is a cluster of of associated objects we treat as a unit for the purpose of data changes. Each aggregate has a root and a boundary. The boundary defines what is inside the aggregate. The root is a single, specific ENTITY contained in the aggregate. The root is the only member of the AGGREGATE that outside objects are allowed to hold references to....

如果您愿意,聚合是对象的 ,其中包括根对象,以及您可以通过遍历本地引用从根对象到达的其他对象。