如果实体成为聚合的根,聚合根是否使用根实体的现有 ID,还是 AR 创建自己的 ID?
在领域驱动设计(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需要 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.