如何读取数据库的动态特性 [英] How to read dynamic properties from database
问题描述
我会尽量解释我的方案的最好方式,我可以。我在我的数据库下表:
产品{的ProductId,的CategoryId ...}
分类{的CategoryId ...}
CategoryProperties {CategoryPropertyId,的CategoryId,名...}
PropertyValues {的ProductId,CategoryPropertyId,价值...}
所以目标是有产品清单属于一些类别和每个类别可以具有在'PropertyValues'表'n'个多个属性与值。我有一个基于的categoryId'返回数据,所以我总是可以从数据库中获得不同的结果的查询:
有关CPU的类别:
英特尔i7处理器| CPU |型号|插座| L1现金|二级护理安老院|等等
有关硬盘类别:
三星F3 | HDD |型号|速度| GB |等等
因此,基于分类从我的查询结果,我总是可以得到不同的列编号和名称。对于数据库访问我用简单的ADO.NET调用存储过程返回结果。 但由于查询的结果是动态的,它的本质,我不知道什么是读取该数据的好办法。
我做了一个产品
的实体,而是我很困惑如何使之真正做到:(
我想,我可以做一个产品
实体,使其中的继承 产品
像其他实体 CPU
,硬盘
,摄像机
, PcCase
, GraphicCard
, MOBILEPHONE
, GPS
等
但我觉得,这是愚蠢,因为我可以用 200 +实体域结束这个。
你会在这种情况下怎么办?
如何阅读和在哪里把这个动态性能?
更新 - 一些解决方案
根据好 @millimoose 的建议为词典
和 @Tim Schmelter 的主意,用数据表
对象我来到了一些解决方案。
现在...这工作我得到的数据看他们,我可以显示它们。
但我仍然需要在意见更聪明的人比我的我是这样做很好或我应该处理这个好还是我做了一些 spageti code 。
所以在这里我所做的:
公共类产品
{
公共产品()
{
this.DynamicProperties =新的名单,其中,字典<字符串,字符串>>();
}
公开名单<字典<字符串,字符串>> DynamicProperties {获得;组; }
}
...
名单<产品>产品=新的名单,其中,产品>();
...
使用(SqlDataAdapter的一个=新的SqlDataAdapter(CMD))
{
数据表T =新的DataTable();
a.Fill(T);
产品p = NULL;
的foreach(DataRow的行t.Rows)
{
P =新产品();
的foreach(COL的DataColumn在t.Columns)
{
字符串属性= col.ColumnName.ToString();
字符串为PropertyValue =行[col.ColumnName]的ToString();
字典<字符串,字符串>词典=新词典<字符串,字符串>();
dictionary.Add(属性,为PropertyValue);
p.DynamicProperties.Add(字典);
}
products.Add(对);
}
}
您有一个产品,一帮类别,每个类别都有一堆属性。
类产品
{
公众诠释编号{获得;组; }
市民分类分类{获取;组; }
公开名单< ProductProperty>属性{获得;组; }
}
类类别
{
公众诠释编号{获得;组; }
公共字符串名称{;组; }
}
类ProductProperty
{
公众诠释编号{获得;组; }
公共字符串名称{;组; }
公共字符串值{获得;组; }
}
您可以在列表/字典存储产品性能
那么你只是为了增加产品的属性。
Properties.Add(新ProductionProperty(){名称=英特尔I7});
或者它的名称/值
Properties.Add(新ProductionProperty(){名称=速度,值=150MB / s的});
I will try to explain my scenario the best way I can. I have the following tables in my database:
Products{ProductId, CategoryId ...}
Categories{CategoryId ...}
CategoryProperties{CategoryPropertyId, CategoryId, Name ...}
PropertyValues{ProductId, CategoryPropertyId, Value ...}
So the goal is to have list of products which belongs to some category and each category can have 'n' number of properties with values in the 'PropertyValues' table. I have a query that returns data based on 'categoryId' so I can always get different results from the database:
For the CPU category:
intel i7 | CPU | Model | Socket | L1 Cash | L2 Cahs | etc.
For the HDD category:
samsung F3 | HDD | Model | Speed | GB | etc.
So based on categories from my query as a result I always can get different column numbers and names. For database access I use simple ADO.NET call to stored procedure that return result. But because query result is dynamic in it's nature I don't know what is a good way to read this data.
I made a Product
entity but I am confused how to make it really :(
I thought that I can make a Product
entity and make other entities which inherit Product
like Cpu
, Hdd
, Camera
, PcCase
, GraphicCard
, MobilePhone
, Gps
etc.
but I think that it's stupid because I can end this with 200+ entities in domain.
What would you do in this situation?
How to read and where to put this dynamic properties?
UPDATE - some solution
All right based on @millimoose suggestion for Dictionary
and @Tim Schmelter idea to use DataTable
object I came to some solution.
Now... this works I get data read them and I can display them.
But I still need advice from smarter people than me on am I did this good or should I handle this better or I am made some spageti code.
So here what I did:
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);
}
}
You have a product, a bunch of categories, and each category has a bunch of properties.
class Product
{
public int Id { get; set; }
public Category Category { get; set; }
public List<ProductProperty> Properties { get; set; }
}
class Category
{
public int Id { get; set; }
public string Name { get; set; }
}
class ProductProperty
{
public int Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
you could store the product properties in a list / dictionary
then you just add properties to the product
Properties.Add(new ProductionProperty() { Name="intel i7"});
or if its name/value
Properties.Add(new ProductionProperty() { Name="Speed", Value="150MB/s"});
这篇关于如何读取数据库的动态特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!