views:

167

answers:

2

According to Dylan's answer for the questions 'Using The Repository Pattern, Is It Best To Save Parent and Children Objects Together Or Separately?', a repository saves the entire aggregate (parent and children).

How does one go about this, would this be coded in the actually stored procedure (if using them); would you call the child repositories as well...

for instance, having a parent class Country with a list of City children:

class Country
{
    List<City> cities;
    ...
}

and a repository snippet:

public Save(Country country)
{
    ...

    ICityRepository cityRepository = new CityRepository();

    foreach (City city in country.Cities)
    {
        cityRepository.Save(city);
    }

    ...
}

or is there a better alternative?

Also, does calling the child repository as in the example above, have heavy performance implications if there are a lot of children?

A: 

You can pass the city objects to the country stored procedure as xml.

Then it just takes one update/insert/delete query to fix them all.

Adam A
+1  A: 

If the Country is the aggregate root, which in your case it looks like it is, then you shouldn't have a separate repository for Cities. Repositories are only necessary for aggregate roots.

So forget the ICityRepository/CityRepository and just save the Cities themselves in your Country repository.

Michael Hart