这就是我应该如何建模域类吗

发布于 2024-12-05 09:49:03 字数 797 浏览 2 评论 0原文

在我的数据库中,我有表 tblCountry 和 tblCity。它们是1:N的关系。在我的领域项目中,我用 City 和 Country 类来表示它们。我真的需要城市类中的 CountryId 还是仅需要 Country 对象?
城市类:

public class City
{
   public int CityId {get;set;}
   public string Name {get;set;}
   public double Longitude {get;set;}
   public double Latitude {get;set;}

   // This confuse me... is this modeled fine?
   public int CountryId {get;set;}
   public Country Country {get;set;}
}

国家类

public class Country
{
   public int CountryId {get;set;}
   public string Name {get;set;}
   public IEnumerable<City> Cities {get;set;}
}

我填充城市对象如下:

...
   City myCity = GetCityByCityId(cityId);
   myCity.Country = GetCountryByCountryId(myCity.CountryId);

   return myCity;
...

In my database I have tables tblCountry and tblCity. They are in 1:N relation. In my domain project I represent them by City and Country classes. Do I realy need CountryId in city class or just Country object?
City class:

public class City
{
   public int CityId {get;set;}
   public string Name {get;set;}
   public double Longitude {get;set;}
   public double Latitude {get;set;}

   // This confuse me... is this modeled fine?
   public int CountryId {get;set;}
   public Country Country {get;set;}
}

Country class

public class Country
{
   public int CountryId {get;set;}
   public string Name {get;set;}
   public IEnumerable<City> Cities {get;set;}
}

I populate city object something like this:

...
   City myCity = GetCityByCityId(cityId);
   myCity.Country = GetCountryByCountryId(myCity.CountryId);

   return myCity;
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天邊彩虹 2024-12-12 09:49:03

我真的需要城市类中的 CountryId 还是仅需要 Country 对象?

域关系是“城市位于国家”。代码应尽可能基于域。您的 City 类将引用 Country 对象:

class City {
    private Country _country;
}

您不应该在城市中拥有 CountryId,因为这是一个持久性技术问题。它应该由数据访问层(ORM)为您处理。

Do I realy need CountryId in city class or just Country object?

The domain relationship is "City is located in the Country". The code should be based on domain as much as possible. Your City class will have a reference to the Country object:

class City {
    private Country _country;
}

You should not have CountryId in the city because it is a persistence technicality. It should be handled by data access layer for you (ORM).

黎歌 2024-12-12 09:49:03

在这种情况下,国家/地区是一个聚合根,它应该填充所有包含的城市,然后您可以简单地获取所需的国家/地区并在聚合根内查找城市。

public class City
{
   public int Id {get;set;}
   public string Name {get;set;}
   public double Longitude {get;set;}
   public double Latitude {get;set;}

   public City(Country country)
   { this.Country = country; }
}

public class Country
{
   public int Id {get;set;}
   public string Name {get;set;}
   public IEnumerable<City> Cities {get;set;}
}

...

   Country myCountry = repository.GetCountryByID(xyz); // return a country with all cities filled

   City myCity =  myCountry.Cities.First(c => c.Id = cityId);

   return myCity;

...

取决于设计,如果城市是聚合根,那么设计将是

public class City
{
   public int Id {get;set;}
   public string Name {get;set;}
   public double Longitude {get;set;}
   public double Latitude {get;set;}
   public Country Country {get;set;}
}

public class Country
{
   public int Id {get;set;}
   public string Name {get;set;}
}

...

   City myCity = repository.GetCityByID(xyz); // return a city with the associated country

   Country myCountry =  myCity.Country;

   return myCity;

...

In this case Country is an aggregate root, it should be filled with all contained cities then you can simply get the country you want and find cities inside the aggregate root.

public class City
{
   public int Id {get;set;}
   public string Name {get;set;}
   public double Longitude {get;set;}
   public double Latitude {get;set;}

   public City(Country country)
   { this.Country = country; }
}

public class Country
{
   public int Id {get;set;}
   public string Name {get;set;}
   public IEnumerable<City> Cities {get;set;}
}

...

   Country myCountry = repository.GetCountryByID(xyz); // return a country with all cities filled

   City myCity =  myCountry.Cities.First(c => c.Id = cityId);

   return myCity;

...

Depending on the design if the City is the aggregate root then the design would be

public class City
{
   public int Id {get;set;}
   public string Name {get;set;}
   public double Longitude {get;set;}
   public double Latitude {get;set;}
   public Country Country {get;set;}
}

public class Country
{
   public int Id {get;set;}
   public string Name {get;set;}
}

...

   City myCity = repository.GetCityByID(xyz); // return a city with the associated country

   Country myCountry =  myCity.Country;

   return myCity;

...

童话里做英雄 2024-12-12 09:49:03

我更喜欢以下内容:

public class City
{
   public int CityId {get;set;}
   public string Name {get;set;}
   public double Longitude {get;set;}
   public double Latitude {get;set;}


   public int CountryId {get;set;}
   public Country Country {get;set;}
public void LoadCountryFromDB()
{
      this.Country = GetCountryByCountryId(this.CountryId);

}
}

有很多数据层生成工具和模型以及代码生成:
CSLAGenMyGeneration 这是 ORM 工具(对象关系映射)之一。
尝试去寻找他们。

I prefer the following :

public class City
{
   public int CityId {get;set;}
   public string Name {get;set;}
   public double Longitude {get;set;}
   public double Latitude {get;set;}


   public int CountryId {get;set;}
   public Country Country {get;set;}
public void LoadCountryFromDB()
{
      this.Country = GetCountryByCountryId(this.CountryId);

}
}

there is a lot of Data Layer Generation Tools and Models and Code Generation :
CSLAGen and MyGeneration which is one of ORM Tools (Object relation Mapping).
try To search for them .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文