Proper reconstitution of Aggregate objects in the Repository?
- by Jebb
Assuming that no ORM (e.g. Doctrine) is used inside the Repository, my question is what is the proper way of instantiating the Aggregate objects? Is it instantiating the child objects directly inside the Repository and just assign it to the Aggregate Root through its setters or the Aggregate Root is responsible of constructing its child entities/objects?
Example 1:
class UserRepository
{
// Create user domain entity.
$user = new User();
$user->setName('Juan');
// Create child object orders entity.
$orders = new Orders($orders);
$user->setOrders($orders);
}
Example 2:
class UserRepository
{
// Create user domain entity.
$user = new User();
$user->setName('Juan');
// Get orders.
$orders = $ordersDao->findByUser(1);
$user->setOrders($orders);
}
whereas in example 2, instantiation of orders are taken care inside the user entity.