简体   繁体   English

汇总根和存储库

[英]Aggregate Roots & Repositories

I'm currently trying to discover the idea behind aggregate roots and their relation to repositories. 我目前正在尝试发现聚合根及其与存储库之间关系的想法。

Given are the following domain entities: 给出以下域实体:

public class Country {

    public string Name { get; set; }
    public ICollection<City> Cities { get; set; }

}

public class City {

    public string Name { get; set; }

}

I guess I correctly identified Country as an aggregate root for City since in my domain, there shouldn't be any cities that are not located within a country. 我猜我正确地将国家/地区标识为城市总根 ,因为在我的域中,不应有不在国家/地区内的任何城市。 It should be only possible to add a new city to the data storage via a country and if a country get's deleted, every city inside should be removed too. 只能通过一个国家/地区将新城市添加到数据存储中,如果删除了一个国家/地区,则也应移除其中的每个城市。

Now, how does such a Country Repository might look like? 现在,这样的国家/地区存储库可能是什么样子? And how does the Country Aggregate Root look like? 国家汇总根的外观如何? Is there a CityRepository inside the domain (which would allow me to add a city to the database even if there is no related country!)? 域内是否有CityRepository (即使没有相关国家,这也允许我向数据库中添加城市!)? And is there a CountryRepository inside the Country (somehow the country needs to populate it's cities? Or is this the job of a repository?) 并且在国家内部是否存在CountryRepository (以某种方式该国家需要填充其城市?还是这是存储库的工作?)

I think you have the hang of it. 我认为您掌握了它。 In your case where you describe: 在您描述的情况下:

since in my domain, there shouldn't be any cities that are not located within a country. 因为在我的领域内,不应有一个国家或地区以外的城市。 It should be only possible to add a new city to the data storage via a country and if a country get's deleted, every city inside should be removed too 只能通过一个国家/地区将新城市添加到数据存储中,如果删除了一个国家/地区,则也应移除其中的每个城市

You are correct, the country is the aggregate root. 您是正确的,国家是总根。 Your country entity code is the aggregate root and it is correct! 您所在国家/地区的实体代码是汇总根,它是正确的! You would not want a city repo. 您不想要城市回购。 Your country repo does not really change depending on the related value objects (in this case cities) or entities, so in this case your basic crud api would look like: 您所在国家/地区的repo并不会真正根据相关的价值对象(在这种情况下为城市)或实体而变化,因此在这种情况下,您的基本Crud API如下所示:

public class CountryRepo {
    public Country GetCountry(String name);
    public Country SaveCourntry(Country country);
    public void DeleteCourntry(Country country);
    ...etc...
}

To touch on your last question: 触摸最后一个问题:

And is there a CountryRepository inside the Country (somehow the country needs to populate it's cities? Or is this the job of a repository?) 并且在国家内部是否存在CountryRepository(以某种方式该国家需要填充其城市?还是这是存储库的工作?)

Your repository will take care of the activity (handling of your entities). 您的存储库将负责该活动(实体的处理)。 Populating cities would be done in the entity by maintaining the collection. 可以通过维护集合在实体中完成城市填充。 You might make a method on your Country entity to add city: 您可以在“国家/地区”实体上添加城市的方法:

public void AddCity(City city)
{
    this.Cities.Add(city);
}

remove would be similar, then you use your repo to save the country which should take care of persisting the related collection of cities. 删除将是类似的,那么您可以使用您的存储库来保存国家/地区,该国家/地区应保留相关的城市集合。

The choice of aggregate root isn't based solely on a hierarchical data relationship. 聚合根的选择并非仅基于分层数据关系。 If your application may access City directly and perform operations on City without using Country (except, of course, the add and delete operations that you mention), then Country is not the aggregate root for City. 如果您的应用程序可以直接访问城市并在不使用国家的情况下在城市上执行操作(当然,您提到的添加和删除操作除外),则国家不是城市的汇总根。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM