如果实体成为聚合的根,聚合根是否使用根实体的现有 ID,还是 AR 创建自己的 ID?

发布于 2024-11-09 09:32:33 字数 739 浏览 0 评论 0原文

在领域驱动设计(DDD)中,实体总是有自己独特的身份。

在我对 DDD 的阅读中,我看到了似乎混合了实体和聚合根之间“唯一身份”概念的陈述和示例。根据示例,它们可能暗示:

  • 我只需要以下接口之一。

或者

  • 我两者都需要。

我想知道就“Eric Evans 型 DDD”而言,哪种方法是“正确的”。

例如,假设您的实体实现了此接口,并在调用此方法时返回 GUID:

public interface IEntity
{
   object IdThatIsUniqueForThisEntityObject { get; }
}

您是否也需要下面的一个?

public interface IAggregateRoot
{
   object IdThatIsUniqueForThisAggregateRootObject { get; }
}

聚合根是否需要实现类似上述接口的接口,以便它可以表示其自己的唯一 ID (IdThatIsUniqueForThisAggregateRootObject),该 ID 与其根实体的 ID (IdThatIsUniqueForThisEntityObject) 是分开且不同的?

或者聚合根应该只使用根实体 (IdThatIsUniqueForThisEntityObject) 来表示聚合根的唯一 ID?

In Domain Driven Design (DDD), an Entity always has its own unique identity.

In my reading on DDD I have seen statements and examples that seem to mix the concepts of "unique identity" between Entities and Aggregate Roots. Depending on the example, they can imply that:

  • I only need one of the interfaces below.

or

  • I need both.

I would like to know which approach is "correct" as far as "Eric Evans type DDD" is concerned.

For example, let's say your Entities implement this interface and return a GUID when this method is called:

public interface IEntity
{
   object IdThatIsUniqueForThisEntityObject { get; }
}

Do you need the one below too or not?

public interface IAggregateRoot
{
   object IdThatIsUniqueForThisAggregateRootObject { get; }
}

Does the Aggregate Root need to implement an interface like the one above so that it can represent its own unique ID (IdThatIsUniqueForThisAggregateRootObject) that is separate and different from its root Entity's ID (IdThatIsUniqueForThisEntityObject)?

Or should the Aggregate Root just use the root Entity's (IdThatIsUniqueForThisEntityObject) to represent the Unique ID of the Aggregate Root?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒江雪… 2024-11-16 09:32:34

您只需要 EntityId。没有创建额外的聚合根标识。聚合根是用于控制访问并将对象组织为数据库工作的单个单元的聚合。聚合不会为对象赋予任何类型的“新”或附加标识。

一个实体始终拥有其唯一的实体标识,这应该足够了。根据定义,检索作为聚合根的实体的操作也会检索聚合。不存在实体的概念,有时只是独立实体,有时是聚合根。因此根本不需要“聚合根身份”的概念。

You only need the EntityId. There is no additional Aggregate Root Identity that is created. An aggregate root is an aggregate that is used to control access and organize objects as a single unit of database work. The aggregate does not confer any sort of "new" or additional identity for an object.

An entity always has it's unique entity identity and that should be sufficient. By definition, an operation that retrieves an entity which is an aggregate root also retrieves the aggregate in doing so. There is no concept of an entity which is sometimes just the standalone entity, and other times the aggregate root. So there is also no need for a concept of "Aggregate Root Identity" at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文