DDD 中聚合存储库之间的关系

Relations between Repositories for Aggragates in DDD

我正在为聚合构建存储库。我们有 3 个不同的实体,它是由 3 个不同的实体构成的,其中之一是根。

所有 3 个数据都保存在 SQL 数据库中。每个都有自己的 table。

让我们考虑获取这些聚合的完整列表的简单情况。我需要从所有 3 table 中获取数据。我应该构建一个优化的查询来获取此数据集,还是应该将每个实体的逻辑封装在它自己的存储库中并 assemble 它是聚合的存储库? (然后聚合回购将调用各自的回购和 assemble 它)

我倾向于第一个解决方案,但它的耦合更强。从 OOP 的角度来看,后者似乎更好,但似乎过于复杂,并且可能会导致后续数据集的缓存失效等问题

For each type of object requiring global access, create an object that provides the illusion of all objects of this type stored in memory. Configure access through the global interface. [..] Define methods for adding and removing objects. [..] Define repositories only for aggregates. ~ Evans, about repositories

您应该仅为聚合创建一个存储库。没有理由创建单独的存储库。更重要的是,创建单独的存储库会导致您提到的一些其他问题。

I'm leaning twords the first solution, however it's stronger coupleing.

要回答这个问题,请查看 Aggregate definition from Martin Fowler:

Aggregate is a pattern in Domain-Driven Design. A DDD aggregate is a cluster of domain objects that can be treated as a single unit. An example may be an order and its line-items, these will be separate objects, but it's useful to treat the order (together with its line items) as a single aggregate.

根据定义.

,聚合正在耦合实体