开发者

Is this how I should model domain classes

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 modele开发者_Go百科d 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;
...


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).


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;

...


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 .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜