generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ODataMapperT.cs
346 lines (294 loc) · 18.2 KB
/
ODataMapperT.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// 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.OData.Mapping
{
/// <summary>
/// Provides bidirectional mapping from a <typeparamref name="TSource"/> <see cref="Type"/> to an <see cref="ODataItem"/>.
/// </summary>
/// <typeparam name="TSource">The source <see cref="Type"/>.</typeparam>
public class ODataMapper<TSource> : IODataMapper<TSource>, IBidirectionalMapper<TSource, ODataItem> where TSource : class, new()
{
private readonly List<IPropertyColumnMapper> _mappings = [];
private readonly bool _implementsIIdentifier = typeof(IIdentifier).IsAssignableFrom(typeof(TSource));
private readonly Lazy<SourceToItemMapper> _mapperFromTo;
private readonly Lazy<ItemToSourceMapper> _mapperToFrom;
/// <summary>
/// Initializes a new instance of the <see cref="ODataMapper{TSource}"/> class.
/// </summary>
/// <param name="autoMap">Indicates whether the entity should automatically map all public get/set properties, where the property and column names are all assumed to share the same name.</param>
/// <param name="ignoreSrceProperties">An array of source property names to ignore.</param>
public ODataMapper(bool autoMap = false, params string[] ignoreSrceProperties)
{
if (typeof(TSource) == typeof(string)) throw new InvalidOperationException("TSource must not be a String.");
if (autoMap)
AutomagicallyMap(ignoreSrceProperties);
_mapperFromTo = new Lazy<SourceToItemMapper>(() => new SourceToItemMapper(this));
_mapperToFrom = new Lazy<ItemToSourceMapper>(() => new ItemToSourceMapper(this));
}
/// <summary>
/// Gets the <see cref="IPropertyColumnMapper"/> mappings.
/// </summary>
public IEnumerable<IPropertyColumnMapper> 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 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(ODataMapper<TSource>)
.GetMethod(nameof(AutoProperty), BindingFlags.NonPublic | BindingFlags.Instance)!
.MakeGenericMethod([sp.PropertyType])
.Invoke(this, [sex, null, OperationTypes.Any]);
}
}
/// <summary>
/// Adds a <see cref="PropertyColumnMapper{TSource, TSourceProperty}"/> to the mapper with additiional auto-logic.
/// </summary>
private PropertyColumnMapper<TSource, TSourceProperty> AutoProperty<TSourceProperty>(Expression<Func<TSource, TSourceProperty>> propertyExpression, string? columnName = null, OperationTypes operationTypes = OperationTypes.Any)
=> Property(propertyExpression, columnName, operationTypes);
/// <summary>
/// Adds a <see cref="PropertyColumnMapper{TSource, TSourceProperty}"/> to the mapper (same as <see cref="Map"/>).
/// </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 <i>OData</i> column name. Defaults to <paramref name="propertyExpression"/> name.</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, OperationTypes operationTypes = OperationTypes.Any)
{
var pcm = new PropertyColumnMapper<TSource, TSourceProperty>(propertyExpression, columnName, operationTypes);
AddMapping(pcm);
return pcm;
}
/// <summary>
/// Adds a <see cref="PropertyColumnMapper{TSource, TSourceProperty}"/> to the mapper (same as <see cref="Property"/>).
/// </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 <i>OData</i> column name. Defaults to <paramref name="propertyExpression"/> name.</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> Map<TSourceProperty>(Expression<Func<TSource, TSourceProperty>> propertyExpression, string? columnName = null, OperationTypes operationTypes = OperationTypes.Any)
=> Property(propertyExpression, columnName, operationTypes);
/// <summary>
/// Validates and adds a new IPropertyColumnMapper.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2208:Instantiate argument exceptions correctly", Justification = "They are the arguments from the calling method.")]
private void AddMapping(IPropertyColumnMapper pcm)
{
if (_mappings.Any(x => x.PropertyName == pcm.PropertyName))
throw new ArgumentException($"Source property '{pcm.PropertyName}' must not be specified more than once.", "propertyExpression");
if (_mappings.Any(x => x.ColumnName == pcm.ColumnName))
throw new ArgumentException($"Column '{pcm.ColumnName}' must not be specified more than once.", "columnName");
_mappings.Add(pcm);
}
/// <summary>
/// Adds or updates <see cref="PropertyColumnMapper{TSource, TSourceProperty}"/> to the mapper (same as <see cref="HasMap"/>)
/// </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 <i>OData</i> column name. Defaults to <paramref name="propertyExpression"/> name.</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"/> and <paramref name="operationTypes"/> where specified will override the previous values.</remarks>
public ODataMapper<TSource> HasProperty<TSourceProperty>(Expression<Func<TSource, TSourceProperty>> propertyExpression, string? columnName = null, OperationTypes? operationTypes = null, Action<PropertyColumnMapper<TSource, TSourceProperty>>? property = null)
{
var tmp = new PropertyColumnMapper<TSource, TSourceProperty>(propertyExpression, columnName, 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 (operationTypes != null)
pcm.OperationTypes = operationTypes.Value;
}
property?.Invoke(pcm);
return this;
}
/// <summary>
/// Adds or updates <see cref="PropertyColumnMapper{TSource, TSourceProperty}"/> to the mapper (same as <see cref="HasProperty"/>).
/// </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 <i>OData</i> column name. Defaults to <paramref name="propertyExpression"/> name.</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"/> and <paramref name="operationTypes"/> where specified will override the previous values.</remarks>
public ODataMapper<TSource> HasMap<TSourceProperty>(Expression<Func<TSource, TSourceProperty>> propertyExpression, string? columnName = null, OperationTypes? operationTypes = null, Action<PropertyColumnMapper<TSource, TSourceProperty>>? property = null)
=> HasProperty(propertyExpression, columnName, operationTypes, property);
/// <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="IODataMapper{T}"/> to inherit from.</param>
public void InheritPropertiesFrom<T>(IODataMapper<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));
var pe = Expression.Parameter(typeof(TSource), "x");
var type = typeof(ODataMapper<>).MakeGenericType(typeof(TSource));
foreach (var p in inheritMapper.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.OperationTypes])!;
if (p.IsPrimaryKey)
pmap.SetPrimaryKey();
if (p.Converter != null)
pmap.SetConverter(p.Converter);
if (p.Mapper != null)
pmap.SetMapper(p.Mapper);
}
}
/// <inheritdoc/>
public void MapToOData(TSource? value, ODataItem entity, OperationTypes operationType = OperationTypes.Unspecified)
{
entity.ThrowIfNull(nameof(entity));
if (value == null) return;
foreach (var p in _mappings)
{
p.MapToOData(value, entity, operationType);
}
OnMapToOData(value, entity, operationType);
}
/// <summary>
/// Extension opportunity when performing a <see cref="MapToOData(TSource, ODataItem, OperationTypes)"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="entity">The <see cref="ODataItem"/> 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 OnMapToOData(TSource value, ODataItem entity, OperationTypes operationType) { }
/// <inheritdoc/>
public TSource? MapFromOData(ODataItem entity, OperationTypes operationType = OperationTypes.Unspecified)
{
entity.ThrowIfNull(nameof(entity));
var value = new TSource();
foreach (var p in _mappings)
{
p.MapFromOData(entity, value, operationType);
}
value = OnMapFromOData(value, entity, operationType);
return (value != null && value is IInitial ii && ii.IsInitial) ? null : value;
}
/// <summary>
/// Extension opportunity when performing a <see cref="MapFromOData(ODataItem, OperationTypes)"/>.
/// </summary>
/// <param name="value">The source value.</param>
/// <param name="entity">The <see cref="ODataItem"/>.</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? OnMapFromOData(TSource value, ODataItem entity, OperationTypes operationType) => value;
/// <summary>
/// <typeparamref name="TSource"/> to <see cref="ODataItem"/> mapper.
/// </summary>
private class SourceToItemMapper(ODataMapper<TSource> parent) : Mapper<TSource, ODataItem>
{
internal ODataMapper<TSource> Parent { get; } = parent;
protected override ODataItem? OnMap(TSource? source, ODataItem? destination, OperationTypes operationType)
{
if (source is null)
return default;
destination ??= new ODataItem();
Parent.MapToOData(source, destination, operationType);
return destination;
}
}
/// <summary>
/// <see cref="ODataItem"/> to <typeparamref name="TSource"/> mapper.
/// </summary>
private class ItemToSourceMapper(ODataMapper<TSource> parent) : Mapper<ODataItem, TSource>
{
internal ODataMapper<TSource> Parent { get; } = parent;
protected override TSource? OnMap(ODataItem? source, TSource? destination, OperationTypes operationType)
{
if (source is null)
return default;
return Parent.MapFromOData(source, operationType);
}
}
/// <inheritdoc/>
IMapper<TSource, ODataItem> IBidirectionalMapper<TSource, ODataItem>.MapperFromTo => _mapperFromTo.Value;
/// <inheritdoc/>
IMapper<ODataItem, TSource> IBidirectionalMapper<TSource, ODataItem>.MapperToFrom => _mapperToFrom.Value;
/// <inheritdoc/>
public object[] GetODataKey(TSource value, OperationTypes operationType = OperationTypes.Unspecified)
{
value.ThrowIfNull(nameof(value));
var km = _mappings.Where(x => x.IsPrimaryKey).ToArray();
if (km.Length == 0)
throw new InvalidOperationException("No primary key mappings have been defined.");
var oi = new ODataItem();
foreach (var p in km)
{
p.MapToOData(value, oi, operationType);
}
var key = new object[km.Length];
for (int i = 0; i < km.Length; i++)
{
key[i] = oi.Attributes[km[i].ColumnName];
}
return key;
}
}
}