1: public abstract class BaseMapper<E>
2: { 3: #region Protected Members
4:
5: protected Dictionary<string, FieldMapped> _specificMapping;
6:
7: #endregion
8:
9: #region Public Methods
10:
11: public virtual void InitializeMapping()
12: { 13: if (_specificMapping == null)
14: { 15: _specificMapping = new Dictionary<string, FieldMapped>();
16:
17: List<FieldInfo> fields = new List<FieldInfo>();
18: List<PropertyInfo> properties = new List<PropertyInfo>();
19:
20: Type type = typeof(E);
21:
22: while (type != typeof(object))
23: { 24: fields.AddRange(type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.Instance));
25: properties.AddRange(type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.Instance));
26: type = type.BaseType;
27: }
28:
29:
30: for (int j = 0; j < fields.Count; j++)
31: { 32: MappingAttribute[] attributes = (MappingAttribute[])fields[j].GetCustomAttributes(typeof(MappingAttribute), true);
33: if (attributes != null && attributes.Length > 0)
34: { 35: if (_specificMapping.ContainsKey(attributes[0].ColumnName))
36: { 37: throw new ApplicationException("The column name is already mapped"); 38: }
39: _specificMapping.Add(attributes[0].ColumnName, new FieldMapped(attributes[0], fields[j], null));
40: }
41: }
42:
43: for (int j = 0; j < properties.Count; j++)
44: { 45: MappingAttribute[] attributes = (MappingAttribute[])properties[j].GetCustomAttributes(typeof(MappingAttribute), true);
46: if (attributes != null && attributes.Length > 0)
47: { 48: if (_specificMapping.ContainsKey(attributes[0].ColumnName))
49: { 50: throw new ApplicationException("The column name is already mapped"); 51: }
52: _specificMapping.Add(attributes[0].ColumnName, new FieldMapped(attributes[0], null, properties[j]));
53: }
54: }
55: }
56: }
57:
58: public abstract ulong Map(E entity, SqlDataReader reader);
59:
60: #endregion
61: }