Proper reconstitution of Aggregate objects in the Repository?

Posted by Jebb on Stack Overflow See other posts from Stack Overflow or by Jebb
Published on 2010-06-07T03:15:48Z Indexed on 2010/06/07 3:22 UTC
Read the original article Hit count: 395

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.

© Stack Overflow or respective owner

Related posts about repository

Related posts about domain-driven-design