【发布时间】:2012-02-26 12:47:22
【问题描述】:
刚刚注册。第一个问题:) 如果我的域模型实体 Country 中有 Name 属性和 List of States 属性。此外,州有名称,州财产清单,进一步,州有自治市,最后是城市实体。
为了更好地解释:我正在尝试使用 CountryDTO 对 Country 实体进行序列化,而我的构造函数看起来像这样:
public CountryDTO(Country x)
{
Name = x.Name;
StateList = new List<StateDTO>();
foreach (State state in x.States)
{
StateDTO stateDto = new StateDTO(state);
StateList.Add(stateDto);
}
}
但是当我深入了解 StateDTO 时,它看起来像这样>
public StateDTO(State x)
{
Name = x.Name;
CountryDTO Country = new CountryDTO(x.Country);
CantonList = new List<CantonDTO>();
foreach (Canton c in x.Cantons)
{
CantonDTO cantonDto = new CantonDTO(c);
CantonList.Add(cantonDto);
}
}
因为我的 StateDTO 应该知道他的 Country 父对象,所以我有这条线 CountryDTO Country = new CountryDTO(x.Country);这是问题所在,(可能是递归引用),这将发生在以 State 对象为父对象的 Cantons 中,等等。 那么如何加载这个父实体并避免这个错误。 我需要引用 Country.Name 之类的父对象,而不是 CountryName 作为字符串。
希望,我已经足够清楚了:)
【问题讨论】: