【发布时间】:2013-02-19 02:58:57
【问题描述】:
我会尽我所能解释我的场景。我的数据库中有以下表格:
Products{ProductId, CategoryId ...}
Categories{CategoryId ...}
CategoryProperties{CategoryPropertyId, CategoryId, Name ...}
PropertyValues{ProductId, CategoryPropertyId, Value ...}
因此,目标是列出属于某个类别的产品,每个类别可以有“n”个属性,其值在“PropertyValues”表中。我有一个基于“categoryId”返回数据的查询,所以我总是可以从数据库中得到不同的结果:
对于 CPU 类别:
intel i7 | CPU | Model | Socket | L1 Cash | L2 Cahs | etc.
对于 HDD 类别:
samsung F3 | HDD | Model | Speed | GB | etc.
因此,根据我的查询中的类别,我总是可以获得不同的列号和名称。对于数据库访问,我使用简单的 ADO.NET 调用来返回结果的存储过程。 但是因为查询结果本质上是动态的,所以我不知道什么是读取这些数据的好方法。
我创建了一个Product 实体,但我很困惑如何真正做到:(
我认为我可以创建一个Product 实体并创建继承 Product 的其他实体,例如Cpu、Hdd、Camera、PcCase、GraphicCard、@987654333 @、Gps 等
但我认为 这很愚蠢,因为我可以在域中使用 200 多个实体 来结束它。
在这种情况下你会怎么做?
如何阅读以及将这个动态属性放在哪里?
更新 - 一些解决方案
好的,基于 @millimoose 建议 Dictionary 和 @Tim Schmelter 使用 DataTable 对象的想法我找到了一些解决方案。
现在...这可以让我读取数据并显示它们。
但是我仍然需要比我更聪明的人提供关于我做得好吗或者我应该更好地处理这件事还是制作一些spageti代码的建议。
所以这就是我所做的:
public class Product
{
public Product()
{
this.DynamicProperties = new List<Dictionary<string, string>>();
}
public List<Dictionary<string, string>> DynamicProperties { get; set; }
}
...
List<Product> products = new List<Product>();
...
using (SqlDataAdapter a = new SqlDataAdapter(cmd))
{
DataTable t = new DataTable();
a.Fill(t);
Product p = null;
foreach (DataRow row in t.Rows)
{
p = new Product();
foreach (DataColumn col in t.Columns)
{
string property = col.ColumnName.ToString();
string propertyValue = row[col.ColumnName].ToString();
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add(property, propertyValue);
p.DynamicProperties.Add(dictionary);
}
products.Add(p);
}
}
【问题讨论】:
-
是否有任何理由需要在模型类中提供这些属性,而不是让
Product有一个Dictionary<string, object>(或类似的东西)? -
最简单的方法是使用
DataTables。然后你只需要一个 sql-query 和一个SqlDataAdapter来填充表格。 -
@millimoose 一开始我想到了
Dictionary。但我不知道这种情况是否有一些不同的解决方案。或者Dictionary实际上是唯一或最好的解决方案。 -
@1110 那么看来您要的是安全毯。您如何尝试使用
Dictionary,这似乎是显而易见的选择,如果这导致您的设计出现问题,请回来询问上述问题? -
@1110 你也可以看看 Steve Yegge 的帖子,他称之为the Properties pattern。免责声明:它很长,而且他有可能每个帖子不止一次出轨。
标签: c# ado.net data-access-layer sqldatareader