• What's an Aggregate Root?


    What's an Aggregate Root?

    回答1

    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.

    回答2

    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.

    Update from Eric Evans: emphasize that aggregate roots are consistency boundaries for transactions/concurrency and deemphasize that outside entities cannot hold references to other aggregate's child entities. – Brian Low Feb 12 '11 at 4:20
     
     
    回答3

    Aggregate root is a complex name for simple idea.


    General idea

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

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

    Example

    Check this simple class hierarchy 

    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 main reason behind aggregate root.


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

    回答4

    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<T> : IRepository<T> 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.

     where T : IAggregateRoot - This one made my day 通过泛型约束确保repository只会操作aggregate root

    DDD_Aggregate

    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.

    An aggregate will have one of its component objects be the aggregate root. Any references from outside the aggregate should only go to the aggregate root. The root can thus ensure the integrity of the aggregate as a whole.

    Aggregates are the basic element of transfer of data storage - you request to load or save whole aggregates. Transactions should not cross aggregate boundaries.

    DDD Aggregates are sometimes confused with collection classes (lists, maps, etc). DDD aggregates are domain concepts (order, clinic visit, playlist), while collections are generic. An aggregate will often contain mutliple collections, together with simple fields. The term "aggregate" is a common one, and is used in various different contexts (e.g. UML), in which case it does not refer to the same concept as a DDD aggregate.

    For more details see the Domain-Driven Design book.

    Aggregates and Aggregate Roots

    Background

    An Aggregate is an object or collection of objects (Entities and Value Objects) that must be treated as a single unit for the purposes of a business transaction. The Aggregate will maintain all invariants for all objects that comprise包含;由…组成 the Aggregate, enforcing all business rules internal to the Aggregate.

    Aggregate Root

    All Aggregates have a single point of entry that is know as the root. The Aggregate Root is the interface to the external world. All interaction with an Aggregate is via the Aggregate Root. As such, an Aggregate Root MUST have a globally unique identifier within the system. Other Entites that are present in the Aggregate but are not Aggregate Roots require only a locally unique identifier, that is, an Id that is unique within the Aggregate.

    For example: an Order will contain Order Lines. The Order is the Aggregate Root and must have a unique Order Id. The Order Lines cannot be accessed directly. Any updates to Order Lines must be via the Order. That is, you ask the Order to make the change to ITS Order Line. An Order Line is only required to be identifiable with the Order therefore it is permissible to have Order Line Ids such as 1, 2, 3 etc. on each order. It is, of course, valid to use a globally unique identifier on each Order Line for persistence purposes, while having a locally unique candidate key for presentation, but it is not required.

    It is not permissible to have references to entities or value objects from an Aggregate Root other than the Aggregate Root itself. In a CQRS system, it is permissible, indeed required, to have read models that have data from one or more Aggregate Roots.

     
     
     
     
     
  • 相关阅读:
    垃圾回收机制,正则模块
    日常模块
    文件路径带有字符串的处理方法
    QT进制之间的相互转换
    4-7 selectors模块
    4-5 异步IO模型
    4-4 多路复用IO模型
    4-3 非阻塞IO
    4-2 阻塞IO
    4-1 IO模型介绍
  • 原文地址:https://www.cnblogs.com/chucklu/p/13091661.html
Copyright © 2020-2023  润新知