What's an Aggregate Root?

前端 未结 11 1340
臣服心动
臣服心动 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:34

    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.

提交回复
热议问题