views:

70

answers:

1

In DDD, Repository takes care of saving and retrieving domain objects and also serves as collection of Aggregate Roots. My question is how do you retrieve the information for those child entities (let's say from DB) of an Aggregate where basic rule is Repository should just hold collection of Aggregate Roots (parent object) and not child objects?

For example:

User (parent object) Orders (child object)

User domain object is stored in user table and Orders are stored in another table.

Basically, retrieving a Domain Object could be like this:

<?php
$userRepos = new UserRepository();
$user = $userRepos->find($userId);
?>

How then the child object (Orders) of User entity be retrieve to be as part of the User aggregate?

+1  A: 

I believe that even though the Repositories only hold references to the Aggregate Roots, the Aggregate Roots will hold references to their child objects (Value objects). So the Factories that are generating the Aggregate objects will build these "internal" references between the Aggregate Roots and the Value objects, and then only the Aggregate Root need be placed in the Repositories. Then it is a simply matter of retrieving these child objects once you pull an Aggregate Root out of the Repository (through a simple getter API).

Eric LaForce
Repositories are to decouple or isolate the Domain from Persistence. Retrieving of single Domain Object (e.g. User) from storage/through a repository (e.g. UserRepository) makes sense, the question is the case with retrieving an Aggregate instead of just one Domain Object, how do you retrieve those child objects that form the Aggregate? Through Repository also? But Repositories are provided for Aggregate Roots only and not for child Objects of the Aggregate.
Jebb
Yes aggregates are stored in the repo like domain objects.In the case of aggregates though they have internal references to their child objects.You should never try and retrieve an aggregates children from the repo directly.In your example you have a User aggregate object with a orders list as a child (the orders associated with a user). In order to do this you first need to retrieve the user object from the repo.Then you can use a getter method on the user object itself to retrieve the orders child object (since the aggregates root is the only object that provides access to the children)
Eric LaForce
Thanks Eric. Yes I agree with you, obtaining/interacting child object (Orders) in an Aggregate should be done through the Aggregate Root (User), maybe the context of my question lies inside the Repository itself, assuming that no ORM (such as Doctrine 2) implemented in the Repository.
Jebb