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).
根据Dylan关于“使用存储库模式,最好是一起还是单独保存父子对象?”的问题的答案,存储库会保存整个聚合(父级和子级)。
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:
例如,拥有一个包含City子列表的父类Country:
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?
此外,如上面的示例中那样调用子存储库,如果有很多孩子,会对性能产生重大影响吗?
2 个解决方案
#1
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.
如果Country是聚合根,在您的情况下它看起来像是,那么您不应该有一个单独的Cities存储库。存储库仅对聚合根来说是必需的。
So forget the ICityRepository/CityRepository and just save the Cities themselves in your Country repository.
因此,忘记ICityRepository / CityRepository,只需将Cities本身保存在您的Country存储库中。
#2
You can pass the city objects to the country stored procedure as xml.
您可以将城市对象作为xml传递给国家/地区存储过程。
Then it just takes one update/insert/delete query to fix them all.
然后它只需要一个更新/插入/删除查询来解决所有问题。
#1
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.
如果Country是聚合根,在您的情况下它看起来像是,那么您不应该有一个单独的Cities存储库。存储库仅对聚合根来说是必需的。
So forget the ICityRepository/CityRepository and just save the Cities themselves in your Country repository.
因此,忘记ICityRepository / CityRepository,只需将Cities本身保存在您的Country存储库中。
#2
You can pass the city objects to the country stored procedure as xml.
您可以将城市对象作为xml传递给国家/地区存储过程。
Then it just takes one update/insert/delete query to fix them all.
然后它只需要一个更新/插入/删除查询来解决所有问题。