Repository navigation
Expand file tree
/
Copy pathClassMapper.cs
More file actions
144 lines (125 loc) · 5.26 KB
/
Copy pathClassMapper.cs
File metadata and controls
144 lines (125 loc) · 5.26 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System.Numerics;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace DapperExtensions.Mapper
{
public interface IClassMapper
{
string SchemaName { get; }
string TableName { get; }
IList<IPropertyMap> Properties { get; }
Type EntityType { get; }
}
public interface IClassMapper<T> : IClassMapper where T : class
{
}
/// <summary>
/// Maps an entity to a table through a collection of property maps.
/// </summary>
public class ClassMapper<T> : IClassMapper<T> where T : class
{
private readonly Dictionary<Type, KeyType> _propertyTypeKeyTypeMapping;
/// <summary>
/// Gets or sets the schema to use when referring to the corresponding table name in the database.
/// </summary>
public string SchemaName { get; protected set; }
/// <summary>
/// Gets or sets the table to use in the database.
/// </summary>
public string TableName { get; protected set; }
/// <summary>
/// A collection of properties that will map to columns in the database table.
/// </summary>
public IList<IPropertyMap> Properties { get; private set; }
public Type EntityType
{
get { return typeof(T); }
}
public ClassMapper()
{
_propertyTypeKeyTypeMapping = new Dictionary<Type, KeyType>
{
{ typeof(byte), KeyType.Identity }, { typeof(byte?), KeyType.Identity },
{ typeof(sbyte), KeyType.Identity }, { typeof(sbyte?), KeyType.Identity },
{ typeof(short), KeyType.Identity }, { typeof(short?), KeyType.Identity },
{ typeof(ushort), KeyType.Identity }, { typeof(ushort?), KeyType.Identity },
{ typeof(int), KeyType.Identity }, { typeof(int?), KeyType.Identity },
{ typeof(uint), KeyType.Identity}, { typeof(uint?), KeyType.Identity },
{ typeof(long), KeyType.Identity }, { typeof(long?), KeyType.Identity },
{ typeof(ulong), KeyType.Identity }, { typeof(ulong?), KeyType.Identity },
{ typeof(BigInteger), KeyType.Identity }, { typeof(BigInteger?), KeyType.Identity },
{ typeof(Guid), KeyType.Guid }, { typeof(Guid?), KeyType.Guid },
};
Properties = new List<IPropertyMap>();
Table(typeof(T).Name);
}
public virtual void Schema(string schemaName)
{
SchemaName = schemaName;
}
public virtual void Table(string tableName)
{
TableName = tableName;
}
protected virtual void AutoMap()
{
AutoMap(null);
}
protected virtual void AutoMap(Func<Type, PropertyInfo, bool> canMap)
{
Type type = typeof(T);
bool hasDefinedKey = Properties.Any(p => p.KeyType != KeyType.NotAKey);
PropertyMap keyMap = null;
foreach (var propertyInfo in type.GetProperties())
{
if (Properties.Any(p => p.Name.Equals(propertyInfo.Name, StringComparison.InvariantCultureIgnoreCase)))
{
continue;
}
if ((canMap != null && !canMap(type, propertyInfo)))
{
continue;
}
PropertyMap map = Map(propertyInfo);
if (!hasDefinedKey)
{
if (string.Equals(map.PropertyInfo.Name, "id", StringComparison.InvariantCultureIgnoreCase))
{
keyMap = map;
}
if (keyMap == null && map.PropertyInfo.Name.EndsWith("id", true, CultureInfo.InvariantCulture))
{
keyMap = map;
}
}
}
if (keyMap != null)
{
keyMap.Key(_propertyTypeKeyTypeMapping.ContainsKey(keyMap.PropertyInfo.PropertyType)
? _propertyTypeKeyTypeMapping[keyMap.PropertyInfo.PropertyType]
: KeyType.Assigned);
}
}
/// <summary>
/// Fluently, maps an entity property to a column
/// </summary>
protected PropertyMap Map(Expression<Func<T, object>> expression)
{
PropertyInfo propertyInfo = ReflectionHelper.GetProperty(expression) as PropertyInfo;
return Map(propertyInfo);
}
/// <summary>
/// Fluently, maps an entity property to a column
/// </summary>
protected PropertyMap Map(PropertyInfo propertyInfo)
{
PropertyMap result = new PropertyMap(propertyInfo);
Properties.Add(result);
return result;
}
}
}