什么是聚合根?[英] What's an Aggregate Root?

本文是小编为大家收集整理的关于什么是聚合根?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我试图围绕如何正确使用存储库模式.总体根源的核心概念不断出现.当搜索网络和堆栈溢出以寻求汇总根本是什么时,我一直在寻找有关它们的讨论以及与应该包含基本定义的页面的链接.

在存储库模式的上下文中,什么是聚集根?

推荐答案

在存储库模式的上下文中,聚合根是您的客户端代码从存储库中加载的唯一对象.

存储库封装了对子对象的访问 - 从呼叫者的角度来看,它会自动加载它们,同时加载根或实际需要时(如懒惰加载).

.

例如,您可能有一个Order对象,该对象将操作封装在多个LineItem对象上.您的客户端代码永远不会直接加载LineItem对象,只有包含它们的Order,这将是您域的该部分的汇总根.

其他推荐答案

来自Evans DDD:

聚合是一个关联对象的群集,我们将其视为数据更改的目的.每个骨料都有根和边界.边界定义了骨料内部的内容.根是骨料中包含的单个特定实体.

和:

根是允许外部对象保存[.]

的唯一汇总成员

这意味着聚集根是可以从存储库中加载的唯一对象.

一个示例是包含Customer实体和Address实体的模型.我们永远不会直接从模型访问Address实体,因为没有关联的Customer上下文是没有意义的.因此,我们可以说Customer和Address一起形成一个聚集体,而Customer是骨料根.

其他推荐答案

聚集根是一个简单想法的复杂名称.


一般想法

设计良好的类图将其内部封装.您访问此结构的点被称为aggregate root.

在此处输入图像说明

您的解决方案的内部可能非常复杂,但是该层次结构的用户只会使用root.doSomethingWhichHasBusinessMeaning().

.

示例

检查这个简单的类层次结构 在此处输入图像描述

您想如何骑车?选择更好的API

选项A(以某种方式起作用):

car.ride();

选项B(用户可以访问类内部):

if(car.getTires().getUsageLevel()< Car.ACCEPTABLE_TIRE_USAGE)
    for (Wheel w: car:getWheels()){
        w.spin();
    }
}

如果您认为该选项A更好,那么恭喜.您会得到aggregate root的主要原因.


聚合根封装多个类.您只能通过主要对象操纵整个层次结构.

本文地址:https://www.itbaoku.cn/post/627446.html

问题描述

I'm trying to get my head around how to properly use the repository pattern. The central concept of an Aggregate Root keeps coming up. When searching both the web and Stack Overflow for help with what an aggregate root is, I keep finding discussions about them and dead links to pages that are supposed to contain base definitions.

In the context of the repository pattern, what is an aggregate root?

推荐答案

In the context of the repository pattern, aggregate roots are the only objects your client code loads from the repository.

The repository encapsulates access to child objects - from a caller's perspective it automatically loads them, either at the same time the root is loaded or when they're actually needed (as with lazy loading).

For example, you might have an Order object which encapsulates operations on multiple LineItem objects. Your client code would never load the LineItem objects directly, just the Order that contains them, which would be the aggregate root for that part of your domain.

其他推荐答案

From Evans DDD:

An AGGREGATE is a cluster of associated objects that 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.

And:

The root is the only member of the AGGREGATE that outside objects are allowed to hold references to[.]

This means that aggregate roots are the only objects that can be loaded from a repository.

An example is a model containing a Customer entity and an Address entity. We would never access an Address entity directly from the model as it does not make sense without the context of an associated Customer. So we could say that Customer and Address together form an aggregate and that Customer is an aggregate root.

其他推荐答案

The aggregate root is a complex name for a simple idea.


General idea

Well designed class diagram encapsulates its internals. Point through which you access this structure is called aggregate root.

enter image description here

Internals of your solution may be very complicated, but users of this hierarchy will just use root.doSomethingWhichHasBusinessMeaning().


Example

Check this simple class hierarchy enter image description here

How do you want to ride your car? Chose better API

Option A (it just somehow works):

car.ride();

Option B (user has access to class inernals):

if(car.getTires().getUsageLevel()< Car.ACCEPTABLE_TIRE_USAGE)
    for (Wheel w: car:getWheels()){
        w.spin();
    }
}

If you think that option A is better then congratulations. You get the main reason behind aggregate root.


Aggregate root encapsulates multiple classes. you can manipulate the whole hierarchy only through the main object.