|
1 | 1 | using System.Linq.Expressions; |
2 | 2 | using System.Reflection; |
3 | 3 |
|
| 4 | +using Microsoft.EntityFrameworkCore.Metadata; |
| 5 | + |
4 | 6 | namespace PhenX.EntityFrameworkCore.BulkInsert.Metadata; |
5 | 7 |
|
6 | 8 | internal static class PropertyAccessor |
7 | 9 | { |
8 | | - public static Func<object, object?> CreateGetter(PropertyInfo propertyInfo, LambdaExpression? converter = null) |
| 10 | + public static Func<object, object?> CreateGetter( |
| 11 | + PropertyInfo propertyInfo, |
| 12 | + IComplexProperty? complexProperty = null, |
| 13 | + LambdaExpression? converter = null) |
9 | 14 | { |
10 | 15 | ArgumentNullException.ThrowIfNull(propertyInfo); |
11 | | - var getMethod = propertyInfo.GetMethod ?? throw new ArgumentException("Property does not have a getter."); |
12 | 16 |
|
| 17 | + // instance => { } |
13 | 18 | var instanceParam = Expression.Parameter(typeof(object), "instance"); |
14 | 19 |
|
15 | | - // Convert object to the declaring type |
16 | | - Expression typedInstance = propertyInfo.DeclaringType!.IsValueType |
17 | | - ? Expression.Unbox(instanceParam, propertyInfo.DeclaringType) |
18 | | - : Expression.Convert(instanceParam, propertyInfo.DeclaringType); |
| 20 | + Expression body; |
| 21 | + |
| 22 | + if (complexProperty == null) |
| 23 | + { |
| 24 | + var propDeclaringType = propertyInfo.DeclaringType!; |
| 25 | + |
| 26 | + // Convert object to the declaring type |
| 27 | + var typedInstance = GetTypedInstance(propDeclaringType, instanceParam); |
| 28 | + |
| 29 | + // instance => ((TEntity)instance).Property |
| 30 | + body = Expression.Property(typedInstance, propertyInfo); |
| 31 | + } |
| 32 | + else |
| 33 | + { |
| 34 | + // Nested access: ((TEntity)instance).ComplexProp.Property |
| 35 | + var complexPropInfo = complexProperty.PropertyInfo!; |
| 36 | + var complexPropDeclaringType = complexPropInfo.DeclaringType!; |
| 37 | + |
| 38 | + var typedInstance = GetTypedInstance(complexPropDeclaringType, instanceParam); |
19 | 39 |
|
20 | | - // Call Getter |
21 | | - Expression getterExpression = Expression.Call(typedInstance, getMethod); |
| 40 | + // instance => ((TEntity)instance).ComplexProp |
| 41 | + Expression complexAccess = Expression.Property(typedInstance, complexPropInfo); |
22 | 42 |
|
23 | | - var propertyType = propertyInfo.PropertyType; |
| 43 | + // instance => ((TEntity)instance).ComplexProp.Property |
| 44 | + body = Expression.Property(complexAccess, propertyInfo); |
| 45 | + } |
24 | 46 |
|
25 | 47 | // If the converter is provided, we call it |
26 | 48 | if (converter != null) |
27 | 49 | { |
28 | 50 | // Validate the converter input type matches property type |
29 | 51 | var converterParamType = converter.Parameters[0].Type; |
30 | | - if (!converterParamType.IsAssignableFrom(propertyType) && !propertyType.IsAssignableFrom(converterParamType)) |
| 52 | + if (!converterParamType.IsAssignableFrom(body.Type) && !body.Type.IsAssignableFrom(converterParamType)) |
31 | 53 | { |
32 | | - throw new ArgumentException($"Converter input must be assignable from property type ({propertyType} -> {converterParamType})"); |
| 54 | + throw new ArgumentException($"Converter input must be assignable from property type ({body.Type} -> {converterParamType})"); |
33 | 55 | } |
34 | 56 |
|
35 | | - // If property type != converter param, convert |
36 | | - var converterInput = getterExpression; |
37 | | - if (converterParamType != propertyType) |
| 57 | + Expression converterInput = body; |
| 58 | + if (converterParamType != body.Type) |
38 | 59 | { |
39 | | - converterInput = Expression.Convert(getterExpression, converterParamType); |
| 60 | + // instance => converter((TConverterType)body) |
| 61 | + converterInput = Expression.Convert(body, converterParamType); |
40 | 62 | } |
41 | 63 |
|
| 64 | + // instance => converter(body) |
42 | 65 | var invokeConverter = Expression.Invoke(converter, converterInput); |
43 | 66 |
|
44 | | - if (propertyType.IsClass) |
| 67 | + if (body.Type.IsClass) |
45 | 68 | { |
46 | | - var nullCondition = Expression.Equal(getterExpression, Expression.Constant(null, propertyType)); |
47 | | - var nullResult = Expression.Constant(null, converter.ReturnType); |
48 | | - getterExpression = Expression.Condition(nullCondition, nullResult, invokeConverter); |
| 69 | + // instance => body == null ? null : converter(body) |
| 70 | + var nullCondition = Expression.Equal(body, Expression.Constant(null, body.Type)); |
| 71 | + var nullResult = Expression.Constant(null, invokeConverter.Type); |
| 72 | + |
| 73 | + body = Expression.Condition(nullCondition, nullResult, invokeConverter); |
49 | 74 | } |
50 | 75 | else |
51 | 76 | { |
52 | | - getterExpression = invokeConverter; |
| 77 | + body = invokeConverter; |
53 | 78 | } |
54 | | - |
55 | | - propertyType = getterExpression.Type; |
56 | 79 | } |
57 | 80 |
|
58 | | - var finalExpression = propertyType.IsValueType |
59 | | - ? Expression.Convert(getterExpression, typeof(object)) |
60 | | - : getterExpression; |
| 81 | + var finalExpression = body.Type.IsValueType |
| 82 | + ? Expression.Convert(body, typeof(object)) |
| 83 | + : body; |
61 | 84 |
|
62 | 85 | return Expression.Lambda<Func<object, object?>>(finalExpression, instanceParam).Compile(); |
63 | 86 | } |
| 87 | + |
| 88 | + private static UnaryExpression GetTypedInstance(Type propDeclaringType, ParameterExpression instanceParam) |
| 89 | + { |
| 90 | + return propDeclaringType.IsValueType |
| 91 | + ? Expression.Unbox(instanceParam, propDeclaringType) |
| 92 | + : Expression.Convert(instanceParam, propDeclaringType); |
| 93 | + } |
64 | 94 | } |
0 commit comments