1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . Linq ;
34using System . Linq . Expressions ;
45using System . Reflection ;
78
89namespace 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 )
0 commit comments