2012-03-13 77 views
0

我想创建一个数据结构以便在我的MVC3应用程序中使用。该网站拥有用户上传的视频,我希望能够为视频设置位置,以便稍后您可以根据国家,地区或城市进行搜索。国家,地区,城市的数据建模

这些实体的建模对我来说不是一个大问题,我的问题是我应该为我的视频实体使用哪个类属性。

public class Country 
{ 
int CountryId 
string CountryName 
} 

public class Region 
{ 
int RegionId 
string RegionName 
int FK_CountryId 
} 

public class City 
{ 
int CityId 
string CityName 
int FK_CountryId 
int FK_RegionId 
} 

........

public class Video 
{ 
int VideoId; 
string VideoName; 
**Location VideoLocation;** 
} 

**public class Location 
{ 
int LocationId; 
Country CountrId; 
Region RegionId; 
City CityId; 
}** 

我最初的想法,但我认为这不是一个很好的设计,因为你可以有2点相同的行了一个位置,在那里应该是保持对位置的唯一参考的理想选择

您认为在良好的设计和性能方面如何?

+0

我会做'国家VideoLocation;'位置重复的就是你已经离开。 – Reniuz 2012-03-13 11:12:47

+0

如果我做国家VideoLocation如何按城市查询例如? – CSharpLearning 2012-03-13 11:15:18

+0

'select * from Video where Video.VideoLocation.CountryId = selectedCity.FK_CountryId' – Reniuz 2012-03-13 11:19:32

回答

0

这是我猜想的每个人的噩梦。那么......至少这是我设计其中一个应用程序时的噩梦。

根据你的情景,你可能会把国家,城市,地区作为不同的实体。一切都是用这种方法找到的,直到你希望用户选择国家,地区或城市。看起来您需要具有空字段,这并不是最佳实践,因为您将不得不完全依赖应用程序逻辑来维护数据完整性。这种做法的

例子是:

public class Country 
{ 
    public string Code { get; set; } //country ID would not make sense in this approach 
    public string Name { get; set; } 
} 

public class Region 
{ 
    public string Code { get; set; } 
    public string Name { get; set; } 
    public string CountryCode { get; set; } //1 region is assigned to only 1 country 
} 

public class City 
{ 
    public string Code { get; set; } 
    public string Name { get; set; } 
    public string RegionCode { get; set; } //1 city is assigned to only 1 region 
} 

它看起来不错,简单易懂但想想,你捕捉什么被选择的表。如果你只关心城市(依赖列表中的最后一项),那一切都很清楚。

public class UserSelectionWithCityOnly 
{ 
    public string CityCode { get; set; } 
} 

很容易和直截了当?看起来是这样。 考虑场景,你可以选择任何一个国家,城市或区域....它得到的真的很乱:

public class UserSelectionWithEitherSelected 
{ 
    public string? CityCode { get; set; } 
    public string? RegionCode { get; set; } 
    public string? CountryCode { get; set; } 
} 

嗯......你总是可以检查是否CityCode.HasValue,但从DB点将是一个可空场,可在其中加脏数据(如果你不是迂腐大约有干净整洁的DB应该是罚款)

所以他们的方式我解决,这是通过创建与父项ID一个阶层表:

public class MySolutionForDestinations 
{ 
    public int DestinationId { get; set; } //primary key 
    public int ParentDestinationId { get; set; } 
    public string Code { get; set; } 
    public string Name { get; set; } 
    public DestinationLevel Level { get; set; } 
} 

public enum DestinationLevel 
{ 
    Country = 0, 
    Region = 1, 
    City = 2 
} 

它可能不是最优雅的解决方案,但它工作得很好。在这种方法中,你只关心DestinationId,它可以是一个国家ID,地区ID或城市ID,所以你肯定会避免有脏数据,并可以实现1对1映射。

希望这将是有用的

+0

谢谢,足够清楚,以适应我自己的情况 – CSharpLearning 2012-03-13 14:12:57