简体   繁体   English

使C#模型更通用所需的替代查找表方法

[英]An alternative lookup table approach needed to make C# models more generic

I currently have the following Models in my EF Code First MVC project (edited for brevity): 我的EF Code First MVC项目中目前有以下模型(为简洁起见进行了编辑):

public class Car
{
    public int Id { get; set; }
    public string Descrip { get; set; }

    // Navigation Property.
    public virtual CarColour CarColour { get; set; }
    ... + numerous other navigation properties.
}

public class CarColour
{
    public int Id { get; set; }
    public string ColourName { get; set; }
}

The CarColour table in the DB contains many rows. 数据库中的CarColour表包含许多行。

In my project, I have about 10 of these sorts of tables, which are essentially lookup tables. 在我的项目中,我大约有10种这类表,它们实质上是查找表。

Rather than have 10 lookup tables (and 10 corresponding 'hard' types in code), I was tasked with implementing a more re-usable approach, instead of having loads of lookup tables, specific to Car (in this example), along the lines of having a couple of tables, one of which may hold the item types (colour, fuel-type etc.) and one which contains the various values for each of the types. 而不是拥有10个查找表(和代码中的10个对应的“硬”类型),我的任务是实现一种更具可重用性的方法,而不是按照特定的路线(例如,在此示例中)加载特定于Car的查找表。其中有几张桌子,其中一张可以容纳物品类型(颜色,燃料类型等),另一张包含每种类型的不同值。 The idea being that our model will be able to be re-used by many other projects - some of which will have potentially hundreds of different attributes, and as such, we won't want to create a new Class/Type in code and generate a new lookup table for each. 想法是我们的​​模型将能够被许多其他项目重用-其中一些项目可能具有数百个不同的属性,因此,我们不想在代码中创建新的Class / Type并生成每个都有一个新的查询表。

I am having difficulty in understanding the c# implementation of this sort of approach and hope someone may be able to give me an example of how this can be achieved in code, more specifically, how the above models would need to change, and what additional classes would be required to accomplish this? 我在理解这种方法的c#实现时遇到困难,希望有人可以给我一个示例,说明如何在代码中实现此目的,更具体地说,上述模型将如何更改,以及需要附加哪些类将需要完成此操作?

your base entity must implement INotifyPropertyChanged and make it generic: 您的基本实体必须实现INotifyPropertyChanged并使其通用:

public virtual CarColour CarColour {
 Get { return this.carColour; }
 Set {
     this.Carcolour; = value
     OnPropertyChanged("CarColour");
     }
}

For more info see : patterns & practices: Prism in CodePlex. 有关更多信息,请参见:模式与实践:CodePlex中的Prism。 http://compositewpf.codeplex.com/wikipage?title=Model%20View%20ViewModel%20(MVVM) http://compositewpf.codeplex.com/wikipage?title=Model%20View%20ViewModel%20(MVVM)

Greetings Bassam 问候巴萨姆

This is not necessarily specific to EF but I've been down this road and didn't really enjoy it. 这不一定特定于EF,但我一直走这条路,并不真正喜欢它。

I wanted to use a single table to represent 'generic' information and while I thought it was smart, it soon showed it's limitations. 我想用一个表来表示“通用”信息,虽然我认为它很聪明,但很快就显示出它的局限性。 One of them being the complexity you need to introduce when writing queries to extract this data if you're performing more than just 'get colours for this car'. 其中之一是,如果您执行的不仅仅是“为这辆车获取颜色”,则在编写查询以提取数据时需要引入复杂性。

I'd say, if your data is simple key/value and the value type is always going to be the same then go for it, it might even be worth having this a mere 'meta-data' for an object: 我想说的是,如果您的数据是简单的键/值,并且值类型总是一样,那么就去做,对于一个对象而言,仅将其作为一个“元数据”可能甚至是值得的:

public class Car
{
    public int Id { get; set; }
    public string Descrip { get; set; }
    public MetaData CarColours { get; set; }
}

public MetaData : Dictionary<int, string>
{
    public MetaData(int group){}
}

Hypothetical table: 假设表:

TableMetaData(int metaGroup, int metaId, string metaValue)

If you're hoping to store different types as your value and may need to perform joining on this data - avoid it and be a bit more specific. 如果您希望存储其他类型作为您的值,并且可能需要对这些数据执行联接,请避免使用它,并且要更加具体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM