创建动态属性/关系

时间:2012-08-21 23:24:46

标签: c# asp.net asp.net-mvc linq-to-sql

我正在使用C#ASP.NET MVC开展企业Web开发项目,他们的一个要求是拥有动态属性以及实体之间的动态关系。因此,他们希望能够在不经历改变SQL模式/ ORM类的麻烦的情况下更改属性/关系。我们正在使用LINQ-to-SQL。

所以我的问题是;实现它的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

void Main()
{
    var db =      @"<root>
                        <entities>
                            <entity id='1' name='tim'/>
                            <entity id='2' name='Gaui'/>
                        </entities>
                        <properties>
                            <property id='1' entityid='1' name='Age' value='46'/>
                            <property id='2' entityid='1' name='Country' value='Australia'/>
                            <property id='3' entityid='2' name='StackoverflowRep' value='17'/>
                        </properties>
                    </root>";
    var doc = XElement.Parse(db);
    var ents = from e in doc.Descendants("entity")
                select new Entity()
                {
                    id = (int)e.Attribute("id"),
                    name = (string)e.Attribute("name"),
                    Properties = doc.Descendants("property")
                                    .Where(p => (int)p.Attribute("entityid") == (int)e.Attribute("id"))
                                    .Select( p => new { name = (string)p.Attribute("name") , value = (string)p.Attribute("value")})
                                    .ToDictionary(k => k.name, v => v.value)
                };
    ents.Dump();                
}


public class Entity
{
    public int id {get;set;}
    public string name {get;set;}
    public IDictionary<string, string> Properties {get;set;}
}

// You can run this spike in LinqPad (language c# Program)

// In this example I just have a simple master detail, and the assumtion is that the name/value pairs are strings. 
// In a real app you probably would have another table with property types, something like...

//<propertytypes>
//  <propertytype id='1' Name='Country' type='string'>
//  ....etc
//  </propertytype>
//</propertytypes>

// and in the properties table you would join to this instead.

// This would give you the data types, plus the ability to 
// populate a list of allowed properties etc.