How to recover aggregated objects?

In DDD, the repository takes on the task of storing and retrieving domain objects, and also serves to collect aggregate roots. My question is, how do you retrieve information for these child objects (say, from the database) from the Aggregate, where the Repository is the main rule, should just store the Aggregate Roots collection (the parent object) and not the child objects?

For instance:

User (parent) Orders (child)

The user domain object is stored in the user table, and Orders are stored in another table.

Basically, getting a domain object can be like this:

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

How then to get the child object (Orders) of the user object as part of the user aggregate?

+3
source share
1 answer

I believe that although the stores store references to Aggregate Roots, Aggregate Roots will reference their child objects (Value objects). Thus, Factories that generate Aggregate objects will build these "internal" links between Aggregate Roots and Value objects, and then only the Aggregate root must be placed in the Repository. Then it is just a matter of restoring these child objects as soon as you pull the Aggregate Root from the repository (via a simple getter API).

+2
source

Source: https://habr.com/ru/post/1748316/


All Articles