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
Aggregate root is a complex name for simple 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()
.
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.