Это как я должен моделировать классы домена - PullRequest
1 голос
/ 19 сентября 2011

В моей базе данных есть таблицы 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;
...

Ответы [ 3 ]

1 голос
/ 19 сентября 2011

В этом случае Страна - это сводный корень, он должен быть заполнен всеми содержащимися в нем городами, тогда вы можете просто получить нужную страну и найти города внутри совокупного корня.

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;

...

В зависимости от дизайна, если City является агрегированным корнем, тогда дизайн будет

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;

...

1 голос
/ 20 сентября 2011

Действительно ли мне нужен CountryId в классе города или просто объект Country?

Доменное отношение "Город находится в стране".Код должен быть максимально основан на домене.Ваш класс City будет иметь ссылку на объект Country:

class City {
    private Country _country;
}

У вас не должно быть CountryId в городе, потому что это постоянство.Он должен обрабатываться для вас уровнем доступа к данным (ORM).

0 голосов
/ 19 сентября 2011

Я предпочитаю следующее:

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

}
}

есть много инструментов и моделей генерации уровня данных и генерации кода: CSLAGen и MyGeneration , что одноORM Tools (Отображение отношений объектов).Попробуйте найти их.

...