forked from FortuneN/Kay.DataAccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TableUtility.cs
102 lines (82 loc) · 2.83 KB
/
TableUtility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Data.Linq.Mapping;
using System.Xml.Linq;
using System.IO;
using System.Data.Linq;
namespace Kay.DataAccess
{
/// <summary>
/// TODO: Update summary.
/// </summary>
public class TableUtility
{
//Fields
private ITable itable;
private DataContext dataContext;
private Type itemType;
private TableAttribute tableAttribute;
private PropertyInfo primaryPropertyInfo;
//Properties
public string TableName { get { return tableAttribute.Name; } }
public string PrimaryKeyName { get { return primaryPropertyInfo.Name; } }
//Contructors
public TableUtility(Type itemType, TableAttribute tableAttribute, PropertyInfo primaryPropertyInfo) : this(null, itemType, tableAttribute, primaryPropertyInfo) { }
public TableUtility(DataContext dataContext, Type itemType, TableAttribute tableAttribute, PropertyInfo primaryPropertyInfo)
{
this.dataContext = dataContext;
if (dataContext != null) this.itable = dataContext.GetTable(itemType);
this.itemType = itemType;
this.tableAttribute = tableAttribute;
this.primaryPropertyInfo = primaryPropertyInfo;
}
//Methods
public object GetPrimaryKeyValue(object item)
{
return primaryPropertyInfo.GetValue(item, null);
}
public void SetPrimaryKeyValue(object item, object value)
{
primaryPropertyInfo.SetValue(item, value, null);
}
public XElement GetRecordSnapshot(object item)
{
Type type = item.GetType();
StringBuilder sbXml = new StringBuilder();
sbXml.AppendFormat("<{0}>", type.Name);
foreach (PropertyInfo propety in type.GetProperties())
{
string propertyTypeName = propety.PropertyType.Name;
if (propertyTypeName.Contains("EntitySet") || propertyTypeName.Contains("EntityRef")) continue;
sbXml.AppendFormat("<{0}><![CDATA[{1}]]></{0}>", propety.Name, propety.GetValue(item, null));
}
sbXml.AppendFormat("</{0}>", type.Name);
XDocument document = XDocument.Load(new StringReader(sbXml.ToString()));
return document.Root;
}
//Static
private static Dictionary<string, TableUtility> cache = new Dictionary<string, TableUtility>();
public static TableUtility GetByType(Type itemType)
{
TableUtility result = null;
string key = itemType.Name;
if (!cache.ContainsKey(key) || (result = cache[key]) == null)
{
TableAttribute table = (TableAttribute)itemType.GetCustomAttributes(typeof(TableAttribute), false)[0];
foreach (PropertyInfo property in itemType.GetProperties())
{
foreach (ColumnAttribute column in property.GetCustomAttributes(typeof(ColumnAttribute), false))
{
if (column.IsPrimaryKey) result = new TableUtility(itemType, table, property);
if (result != null) break;
}
if (result != null) break;
}
cache[key] = result;
}
return result;
}
}
}