Skip to content

Commit 8475979

Browse files
committed
Update 5.0 changes to IObjectMapper
1 parent d45e218 commit 8475979

5 files changed

Lines changed: 20 additions & 25 deletions

File tree

global.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"sdk": {
33
"version": "1.0.0-preview1-002702"
4-
}
4+
},
5+
"projects": []
56
}

src/AutoMapper.Collection-Signed/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
},
1919

2020
"dependencies": {
21-
"AutoMapper": "5.0.0-beta-1"
21+
"AutoMapper": "5.0.0-beta-01111"
2222
},
2323

2424
"frameworks": {

src/AutoMapper.Collection/Mappers/EquivlentExpressionAddRemoveCollectionMapper.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Linq.Expressions;
45
using System.Reflection;
@@ -7,14 +8,15 @@
78

89
namespace AutoMapper.Mappers
910
{
10-
public class EquivlentExpressionAddRemoveCollectionMapper : IObjectMapExpression
11+
public class EquivlentExpressionAddRemoveCollectionMapper : IObjectMapper
1112
{
12-
public static TDestination Map<TSource, TSourceItem, TDestination, TDestinationItem>(TSource source, TDestination destination, ResolutionContext context)
13+
private readonly CollectionMapper CollectionMapper = new CollectionMapper();
14+
public static TDestination Map<TSource, TSourceItem, TDestination, TDestinationItem>(TSource source, TDestination destination, ResolutionContext context, Func<TDestination> ifNullFunc)
1315
where TSource : IEnumerable<TSourceItem>
1416
where TDestination : class, ICollection<TDestinationItem>
1517
{
1618
if (source == null || destination == null)
17-
return CollectionMapper.Map<TSource, TSourceItem, TDestination, TDestinationItem>(source, destination, context);
19+
return ifNullFunc();
1820

1921
var equivilencyExpression = GetEquivilentExpression(new TypePair(typeof(TSource), typeof(TDestination))) as IEquivilentExpression<TSourceItem,TDestinationItem>;
2022
var compareSourceToDestination = source.ToDictionary(s => s, s => destination.FirstOrDefault(d => equivilencyExpression.IsEquivlent(s, d)));
@@ -34,13 +36,7 @@ public static TDestination Map<TSource, TSourceItem, TDestination, TDestinationI
3436
}
3537

3638
private static readonly MethodInfo MapMethodInfo = typeof(EquivlentExpressionAddRemoveCollectionMapper).GetRuntimeMethods().First(_ => _.IsStatic);
37-
38-
public object Map(ResolutionContext context)
39-
{
40-
return
41-
MapMethodInfo.MakeGenericMethod(context.SourceType, TypeHelper.GetElementType(context.SourceType), context.DestinationType, TypeHelper.GetElementType(context.DestinationType))
42-
.Invoke(null, new[] { context.SourceValue, context.DestinationValue, context });
43-
}
39+
4440

4541
public bool IsMatch(TypePair typePair)
4642
{
@@ -49,11 +45,14 @@ public bool IsMatch(TypePair typePair)
4945
&& GetEquivilentExpression(typePair) != null;
5046
}
5147

52-
public Expression MapExpression(Expression sourceExpression, Expression destExpression, Expression contextExpression)
48+
public Expression MapExpression(TypeMapRegistry typeMapRegistry, IConfigurationProvider configurationProvider,
49+
PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression)
5350
{
51+
var collectionExpression = CollectionMapper.MapExpression(typeMapRegistry, configurationProvider, propertyMap, sourceExpression, destExpression, contextExpression);
52+
var collectionFunc = Expression.Lambda(collectionExpression).Compile();
5453
return Expression.Call(null,
5554
MapMethodInfo.MakeGenericMethod(sourceExpression.Type, TypeHelper.GetElementType(sourceExpression.Type), destExpression.Type, TypeHelper.GetElementType(destExpression.Type)),
56-
sourceExpression, destExpression, contextExpression);
55+
sourceExpression, destExpression, contextExpression, Expression.Constant(collectionFunc));
5756
}
5857

5958
private static IEquivilentExpression GetEquivilentExpression(TypePair typePair)

src/AutoMapper.Collection/Mappers/ObjectToEquivalencyExpressionByEquivalencyExistingMapper.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace AutoMapper.Mappers
88
{
9-
public class ObjectToEquivalencyExpressionByEquivalencyExistingMapper : IObjectMapExpression
9+
public class ObjectToEquivalencyExpressionByEquivalencyExistingMapper : IObjectMapper
1010
{
1111
public static Expression<Func<TDestination, bool>> Map<TSource, TDestination>(TSource source)
1212
{
@@ -15,13 +15,7 @@ public static Expression<Func<TDestination, bool>> Map<TSource, TDestination>(TS
1515
}
1616

1717
private static readonly MethodInfo MapMethodInfo = typeof(ObjectToEquivalencyExpressionByEquivalencyExistingMapper).GetRuntimeMethods().First(_ => _.IsStatic);
18-
19-
public object Map(ResolutionContext context)
20-
{
21-
var destExpressArgType = context.DestinationType.GetSinglePredicateExpressionArgumentType();
22-
return MapMethodInfo.MakeGenericMethod(context.SourceType, destExpressArgType).Invoke(null, new[] { context.SourceValue});
23-
}
24-
18+
2519
public bool IsMatch(TypePair typePair)
2620
{
2721
var destExpressArgType = typePair.DestinationType.GetSinglePredicateExpressionArgumentType();
@@ -31,7 +25,8 @@ public bool IsMatch(TypePair typePair)
3125
return expression != null;
3226
}
3327

34-
public Expression MapExpression(Expression sourceExpression, Expression destExpression, Expression contextExpression)
28+
public Expression MapExpression(TypeMapRegistry typeMapRegistry, IConfigurationProvider configurationProvider,
29+
PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression)
3530
{
3631
var destExpressArgType = destExpression.Type.GetSinglePredicateExpressionArgumentType();
3732
return Expression.Call(null, MapMethodInfo.MakeGenericMethod(sourceExpression.Type, destExpressArgType), sourceExpression);

src/AutoMapper.Collection/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111

1212
"dependencies": {
13-
"AutoMapper" : "5.0.0-beta-1"
13+
"AutoMapper": "5.0.0-beta-01111"
1414
},
1515

1616
"frameworks": {

0 commit comments

Comments
 (0)