generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDatabaseMapperT.cs
289 lines (246 loc) · 15.5 KB
/
DatabaseMapperT.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Abstractions.Reflection;
using CoreEx.Entities;
using CoreEx.Mapping;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace CoreEx.Database.Mapping
{
/// <summary>
/// Provides mapping from a <typeparamref name="TSource"/> <see cref="Type"/> and database using reflection.
/// </summary>
/// <typeparam name="TSource">The source <see cref="Type"/>.</typeparam>
/// <remarks>Where performance is critical consider using <see cref="DatabaseMapperEx{TSource}"/>.</remarks>
public class DatabaseMapper<TSource> : IDatabaseMapper<TSource>, IDatabaseMapperMappings where TSource : class, new()
{
private readonly List<IPropertyColumnMapper> _mappings = [];
/// <summary>
/// Initializes a new instance of the <see cref="DatabaseMapper{TSource}"/> class.
/// </summary>
/// <param name="autoMap">Indicates whether the entity should automatically map all public get/set properties, where the property, column and parameter names are all assumed to share the same name.</param>
/// <param name="ignoreSrceProperties">An array of source property names to ignore.</param>
public DatabaseMapper(bool autoMap = false, params string[] ignoreSrceProperties)
{
if (typeof(TSource) == typeof(string)) throw new InvalidOperationException("TSource must not be a String.");
if (autoMap)
AutomagicallyMap(ignoreSrceProperties);
}
/// <inheritdoc/>
IEnumerable<IPropertyColumnMapper> IDatabaseMapperMappings.Mappings => _mappings.AsEnumerable();
/// <inheritdoc/>
public IPropertyColumnMapper this[string propertyName] => TryGetProperty(propertyName, out var pcm) ? pcm : throw new ArgumentException($"Property '{propertyName}' does not exist.", nameof(propertyName));
/// <summary>
/// Gets the <see cref="IPropertyColumnMapper"/> for the specified source <paramref name="propertyExpression"/>.
/// </summary>
/// <param name="propertyExpression">The <see cref="Expression"/> to reference the source property.</param>
/// <returns>The <see cref="IPropertyColumnMapper"/> where found.</returns>
/// <exception cref="ArgumentException">Thrown when the property does not exist.</exception>
public IPropertyColumnMapper this[Expression<Func<TSource, object?>> propertyExpression]
{
get
{
propertyExpression.ThrowIfNull(nameof(propertyExpression));
MemberExpression? me = null;
if (propertyExpression.Body.NodeType == ExpressionType.MemberAccess)
me = propertyExpression.Body as MemberExpression;
else if (propertyExpression.Body.NodeType == ExpressionType.Convert)
{
if (propertyExpression.Body is UnaryExpression ue)
me = ue.Operand as MemberExpression;
}
if (me == null)
throw new InvalidOperationException("Only Member access expressions are supported.");
return this[me.Member.Name];
}
}
/// <inheritdoc/>
public bool TryGetProperty(string propertyName, [NotNullWhen(true)] out IPropertyColumnMapper? propertyColumnMapper)
{
propertyColumnMapper = _mappings.Where(x => x.PropertyName == propertyName).FirstOrDefault();
return propertyColumnMapper != null;
}
/// <inheritdoc/>
public string GetParameterName(string propertyName) => this[propertyName].ParameterName;
/// <inheritdoc/>
public string GetColumnName(string propertyName) => this[propertyName].ColumnName;
/// <summary>
/// Automatically add each public get/set property.
/// </summary>
private void AutomagicallyMap(string[] ignoreSrceProperties)
{
foreach (var sp in TypeReflector.GetProperties(typeof(TSource)))
{
// Do not auto-map where ignore has been specified.
if (ignoreSrceProperties.Contains(sp.Name))
continue;
// Create the lambda expression for the property and add to the mapper.
var spe = Expression.Parameter(typeof(TSource), "x");
var sex = Expression.Lambda(Expression.Property(spe, sp), spe);
typeof(DatabaseMapper<TSource>)
.GetMethod("Property", BindingFlags.Public | BindingFlags.Instance)!
.MakeGenericMethod([sp.PropertyType])
.Invoke(this, [sex, null, null, OperationTypes.Any]);
}
}
/// <summary>
/// Adds a <see cref="PropertyColumnMapper{TSource, TSourceProperty}"/> to the mapper.
/// </summary>
/// <typeparam name="TSourceProperty">The source property <see cref="Type"/>.</typeparam>
/// <param name="propertyExpression">The <see cref="Expression"/> to reference the source property.</param>
/// <param name="columnName">The database column name. Defaults to <paramref name="propertyExpression"/> name.</param>
/// <param name="parameterName">The database parameter name. Defaults to <paramref name="columnName"/> prefixed with '<c>@</c>'.</param>
/// <param name="operationTypes">The <see cref="CoreEx.Mapping.OperationTypes"/> selection to enable inclusion or exclusion of property.</param>
/// <returns>The <see cref="PropertyColumnMapper{TSource, TSourceProperty}"/>.</returns>
public PropertyColumnMapper<TSource, TSourceProperty> Property<TSourceProperty>(Expression<Func<TSource, TSourceProperty>> propertyExpression, string? columnName = null, string? parameterName = null, OperationTypes operationTypes = OperationTypes.Any)
{
var pcm = new PropertyColumnMapper<TSource, TSourceProperty>(propertyExpression, columnName, parameterName, operationTypes);
AddMapping(pcm);
return pcm;
}
/// <summary>
/// Validates and adds a new IPropertyColumnMapper.
/// </summary>
private void AddMapping<TSourceProperty>(PropertyColumnMapper<TSource, TSourceProperty> propertyColumnMapper)
{
if (_mappings.Any(x => x.PropertyName == propertyColumnMapper.PropertyName))
throw new ArgumentException($"Source property '{propertyColumnMapper.PropertyName}' must not be specified more than once.", nameof(propertyColumnMapper));
if (_mappings.Any(x => x.ColumnName == propertyColumnMapper.ColumnName))
throw new ArgumentException($"Column '{propertyColumnMapper.ColumnName}' must not be specified more than once.", nameof(propertyColumnMapper));
if (_mappings.Any(x => x.ParameterName == propertyColumnMapper.ParameterName))
throw new ArgumentException($"Parameter '{propertyColumnMapper.ParameterName}' must not be specified more than once.", nameof(propertyColumnMapper));
_mappings.Add(propertyColumnMapper);
}
/// <summary>
/// Adds or updates <see cref="PropertyColumnMapper{TSource, TSourceProperty}"/> to the mapper.
/// </summary>
/// <typeparam name="TSourceProperty">The source property <see cref="Type"/>.</typeparam>
/// <param name="propertyExpression">The <see cref="Expression"/> to reference the source property.</param>
/// <param name="columnName">The database column name. Defaults to <paramref name="propertyExpression"/> name.</param>
/// <param name="parameterName">The database parameter name. Defaults to <paramref name="columnName"/> prefixed with '<c>@</c>'.</param>
/// <param name="operationTypes">The <see cref="CoreEx.Mapping.OperationTypes"/> selection to enable inclusion or exclusion of property.</param>
/// <param name="property">An <see cref="Action"/> enabling access to the created <see cref="PropertyColumnMapper{TSource, TSourceProperty}"/>.</param>
/// <returns></returns>
/// <remarks>Where updating an existing the <paramref name="columnName"/>, <paramref name="parameterName"/> and <paramref name="operationTypes"/> where specified will override the previous values.</remarks>
public DatabaseMapper<TSource> HasProperty<TSourceProperty>(Expression<Func<TSource, TSourceProperty>> propertyExpression, string? columnName = null, string? parameterName = null, OperationTypes? operationTypes = null, Action<PropertyColumnMapper<TSource, TSourceProperty>>? property = null)
{
var tmp = new PropertyColumnMapper<TSource, TSourceProperty>(propertyExpression, columnName, parameterName, operationTypes ?? OperationTypes.Any);
var pcm = _mappings.Where(x => x.PropertyName == tmp.PropertyName).OfType<PropertyColumnMapper<TSource, TSourceProperty>>().SingleOrDefault();
if (pcm == null)
AddMapping(pcm = tmp);
else
{
if (columnName != null && tmp.ColumnName != pcm.ColumnName)
{
if (_mappings.Any(x => x.ColumnName == pcm.ColumnName))
throw new ArgumentException($"Column '{pcm.ColumnName}' must not be specified more than once.", nameof(columnName));
else
pcm.ColumnName = tmp.ColumnName;
}
if (parameterName != null && tmp.ParameterName != pcm.ParameterName)
{
if (_mappings.Any(x => x.ParameterName == tmp.ParameterName))
throw new ArgumentException($"Parameter '{pcm.ParameterName}' must not be specified more than once.", nameof(parameterName));
else
pcm.ParameterName = tmp.ParameterName;
}
if (operationTypes != null)
pcm.OperationTypes = operationTypes.Value;
}
property?.Invoke(pcm);
return this;
}
/// <summary>
/// Inherits the property mappings from the selected <paramref name="inheritMapper"/>.
/// </summary>
/// <typeparam name="T">The <paramref name="inheritMapper"/> source <see cref="Type"/>. Must inherit from <typeparamref name="TSource"/>.</typeparam>
/// <param name="inheritMapper">The <see cref="IDatabaseMapper{T}"/> to inherit from. Must also implement <see cref="IDatabaseMapperMappings"/>.</param>
public void InheritPropertiesFrom<T>(IDatabaseMapper<T> inheritMapper) where T : class, new()
{
inheritMapper.ThrowIfNull(nameof(inheritMapper));
if (!typeof(TSource).IsSubclassOf(typeof(T))) throw new ArgumentException($"Type {typeof(TSource).Name} must inherit from {typeof(T).Name}.", nameof(inheritMapper));
if (inheritMapper is not IDatabaseMapperMappings inheritMappings) throw new ArgumentException($"Type {typeof(T).Name} must implement {typeof(IDatabaseMapperMappings).Name} to copy the mappings.", nameof(inheritMapper));
var pe = Expression.Parameter(typeof(TSource), "x");
var type = typeof(DatabaseMapper<>).MakeGenericType(typeof(TSource));
foreach (var p in inheritMappings.Mappings)
{
var lex = Expression.Lambda(Expression.Property(pe, p.PropertyName), pe);
var pmap = (IPropertyColumnMapper)type
.GetMethod("Property", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)!
.MakeGenericMethod(p.PropertyType)
.Invoke(this, [lex, p.ColumnName, p.ParameterName, p.OperationTypes])!;
if (p.IsPrimaryKey)
pmap.SetPrimaryKey(p.IsPrimaryKeyGeneratedOnCreate);
if (p.DbType != null)
pmap.SetDbType(p.DbType.Value);
if (p.Converter != null)
pmap.SetConverter(p.Converter);
if (p.Mapper != null)
pmap.SetMapper(p.Mapper);
}
}
/// <inheritdoc/>
public void MapToDb(TSource? value, DatabaseParameterCollection parameters, OperationTypes operationType = OperationTypes.Unspecified)
{
parameters.ThrowIfNull(nameof(parameters));
if (value == null) return;
foreach (var p in _mappings)
{
p.MapToDb(value, parameters, operationType);
}
OnMapToDb(value, parameters, operationType);
}
/// <summary>
/// Extension opportunity when performing a <see cref="MapToDb(TSource, DatabaseParameterCollection, OperationTypes)"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="parameters">The <see cref="DatabaseParameterCollection"/> to update from the <paramref name="value"/>.</param>
/// <param name="operationType">The single <see cref="OperationTypes"/> value being performed to enable conditional execution where appropriate.</param>
protected virtual void OnMapToDb(TSource value, DatabaseParameterCollection parameters, OperationTypes operationType) { }
/// <inheritdoc/>
public TSource? MapFromDb(DatabaseRecord record, OperationTypes operationType = OperationTypes.Unspecified)
{
record.ThrowIfNull(nameof(record));
var value = new TSource();
foreach (var p in _mappings)
{
p.MapFromDb(record, value, operationType);
}
value = OnMapFromDb(value, record, operationType);
return (value != null && value is IInitial ii && ii.IsInitial) ? null : value;
}
/// <summary>
/// Extension opportunity when performing a <see cref="MapFromDb(DatabaseRecord, OperationTypes)"/>.
/// </summary>
/// <param name="value">The source value.</param>
/// <param name="record">The <see cref="DatabaseRecord"/>.</param>
/// <param name="operationType">The single <see cref="OperationTypes"/> value being performed to enable conditional execution where appropriate.</param>
/// <returns>The source value.</returns>
protected virtual TSource? OnMapFromDb(TSource value, DatabaseRecord record, OperationTypes operationType) => value;
/// <summary>
/// Converts the property value for the database.
/// </summary>
private static object? ConvertPropertyValueForDb(IPropertyColumnMapper pcm, object? value) => pcm.Converter == null ? value : pcm.Converter.ConvertToDestination(value);
/// <inheritdoc/>
void IDatabaseMapper.MapKeyToDb(CompositeKey key, DatabaseParameterCollection parameters)
{
parameters.ThrowIfNull(nameof(parameters));
var pk = _mappings.Where(x => x.IsPrimaryKey).ToArray();
if (key.Args == null || key.Args.Length != pk.Length)
throw new ArgumentException($"The number of keys supplied must equal the number of properties identified as {nameof(IPropertyColumnMapper.IsPrimaryKey)}.", nameof(key));
for (int i = 0; i < key.Args.Length; i++)
{
var p = pk[i];
var pval = DatabaseMapper<TSource>.ConvertPropertyValueForDb(p, key.Args[i]);
if (p.DbType.HasValue)
parameters.AddParameter(p.ParameterName, pval, p.DbType.Value);
else
parameters.AddParameter(p.ParameterName, pval);
}
}
}
}