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

huangapple go评论135阅读模式
英文:

Are aggregates and aggregate roots implemented as separate classes?

问题

  1. 它们是独立的类
class Aggregate {
   private Entity aggregateRootEntity;
   // 保持不变性的方法
}
  1. 没有单独的聚合类,而是聚合根类,表示整个聚合
class AggregateRootEntity {
   // id、字段、值对象引用、其他实体引用
   // 保持不变性的方法
}
  1. 任何实体都可以成为聚合,如果在事务范围内使用,保持不变性,并且“聚合”了一些值对象
// 这是一个聚合:
class Entity {
   private List<ValueObject> valueObjects;
   // id、字段
   // 保持不变性的方法
}
英文:

With so much talk about DDD, I've expected to find lot of code examples how these patterns are implemented. But wasn't so lucky.

What confuses me is this. Is an aggregate kind of entity, represented as aggregate root, or are those implemented as separate classes? Here are few examples, please answer if any of them is true.

  1. They are separate classes
class Aggregate {
   private Entity aggregateRootEntity;
   //methods that keep invariants
}
  1. There is no separate aggregate class, but aggregate root class, which represents the aggregate
class AggregateRootEntity {
   //id, fields, value objects references, other entities references
   //methods that keep invariants
}
  1. Any entity can be an aggregate, if it is used in transaction scope, keeps invariants, and "aggregates" some value objects
//This is aggregate:
class Entity {
   private List&lt;ValueObject&gt; valueObjects;
   //id, fields
   //methods that keep invariants
}
   

答案1

得分: 4

> 没有单独的聚合类,而是聚合根类,它表示聚合

这个。

介绍了领域驱动设计(DDD)和"聚合"模式的参考书是埃里克·埃文斯(Eric Evans)的《领域驱动设计:攻克软件之心的复杂性》1

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

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

> 聚合是一组相关对象的集群,我们将其作为数据更改的单元来处理。每个聚合都有一个根和一个边界。边界定义了聚合内部的内容。根是聚合中包含的单个特定实体。根是聚合中外部对象被允许持有引用的唯一成员......

如果愿意,聚合是对象的_图形_,包括根对象和可以通过遍历局部引用从根对象到达的其他对象。

英文:

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

This one.

The reference book that introduced DDD, and the "aggregate" pattern, is Eric Evans Domain Driven Design: Tackling Complexity int the Heart of Software.

Chapter 5 discusses patterns used in modeling a domain in software: entities, values, "domain services", and so on.

Chapter 6 discusses life cycle management, where he talks about factories, repositories, and aggregates:

> 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....

If you like, an aggregate is a graph of objects, which includes the root object, and the other objects that you can reach from the root object by traversing local references.

huangapple
  • 本文由 发表于 2020年8月19日 21:36:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63488232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定