Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using The Repository Pattern, Is It Best To Save Parent and Children Objects Together Or Separately?

Having a parent object Employee with a list of Address child objects:

class Employee
{
    List<Address> addresses;
}

and a Repository method:

void Insert(Employee);

Should the code within this repository attempt to save the parent Employee as well as the child Address objects, or should separate repositories handle the parent and children objects?

If separate repositories, then when saving the Employee object and its children within the client code, should this be separate calls at that level, combined in some sort of service or is there another alternative?

like image 880
Laz Avatar asked Jun 18 '09 00:06

Laz


1 Answers

The repository should handle the entire aggregate object (parent and all children), because that's what makes it a repository. If you're saving root and child objects separately, then the pattern you're using isn't really called a repository (although it might still be a perfectly good solution)

"Repository" is just a convenient name for a particular data access pattern that loads and saves entire aggregates in one go - so when you say to another developer that you're using the repository pattern, they know that that's what's going on.

like image 115
Dylan Beattie Avatar answered Oct 19 '22 08:10

Dylan Beattie