What's an Aggregate Root?

前端 未结 11 1550
臣服心动
臣服心动 2020-11-22 04:18

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

11条回答
  •  半阙折子戏
    2020-11-22 04:53

    Imagine you have a Computer entity, this entity also cannot live without its Software entity and Hardware entity. These form the Computer aggregate, the mini-ecosystem for the Computer portion of the domain.

    Aggregate Root is the mothership entity inside the aggregate (in our case Computer), it is a common practice to have your repository only work with the entities that are Aggregate Roots, and this entity is responsible for initializing the other entities.

    Consider Aggregate Root as an Entry-Point to an Aggregate.

    In C# code:

    public class Computer : IEntity, IAggregateRoot
    {
        public Hardware Hardware { get; set; }
        public Software Software { get; set; }
    }
    
    public class Hardware : IEntity { }
    public class Software : IValueObject { }
    
    public class Repository : IRepository where T : IAggregateRoot {}
    

    Keep in mind that Hardware would likely be a ValueObject too (do not have identity on its own), consider it as an example only.

提交回复
热议问题