I am fairly new to DDD and for me I search in practical terms to a good function for an aggregate. I have a long list of users (User
) and a create an aggregate (UserAggregate
) for that.
Now in my view I might display users based on different criteria. Let's assume here I don't sort the list in my database query, it is OK to have sorting in an aggregate? I cannot come up with another part of the application where I can do this properly, but I might misunderstand the function of an aggregate.
$users = new UserAggregate(array(
$user1, $user2, $user3, $user4
));
foreach ($users->sortBySurname(), $user) {
//
}
foreach ($users->sortByLastLoggedIn(), $user) {
//
}
The same holds for filtering by the way. When I read about DDD aggregates, I immediately thought of the Doctrine\Common\Collections\Collection
(link). That sort-of looks like an aggregate to me. So DDD experts, please enlighten me :-)
Update
The only way I was thinking btw was making aggregates rather immutual and have helpers to create new aggregates. But this seems not the way to go:
$aggregate = new UserAggregate(array($user1, $user2));
$helper = new FilterSomething;
$aggregate = $helper->filter($aggregate);
The second aggregate is a new instance, so the helper looks like this:
class FilterSomething
{
public function filter(Aggregate $aggregate)
{
$items = $aggregate->getItems();
$items = $this->doFilter($items);
return new Aggregate($items);
}
}