@@ -41,16 +41,14 @@ public Expression Create(IObjectMappingData mappingData)
4141 : derivedTypeMappings ;
4242 }
4343
44- var basicMapperData = mapperData . WithNoTargetMember ( ) ;
45- var preMappingCallback = GetMappingCallbackOrNull ( CallbackPosition . Before , basicMapperData , mapperData ) ;
46- var postMappingCallback = GetMappingCallbackOrNull ( CallbackPosition . After , basicMapperData , mapperData ) ;
44+ var mappingDecor = GetMappingExtras ( mapperData ) ;
4745
4846 mappingExpressions . AddUnlessNullOrEmpty ( derivedTypeMappings ) ;
49- mappingExpressions . AddUnlessNullOrEmpty ( preMappingCallback ) ;
47+ mappingExpressions . AddUnlessNullOrEmpty ( mappingDecor . PreMappingCallback ) ;
5048 mappingExpressions . AddRange ( GetObjectPopulation ( mappingData ) ) ;
51- mappingExpressions . AddUnlessNullOrEmpty ( postMappingCallback ) ;
49+ mappingExpressions . AddUnlessNullOrEmpty ( mappingDecor . PostMappingCallback ) ;
5250
53- var mappingBlock = GetMappingBlock ( mappingExpressions , mapperData ) ;
51+ var mappingBlock = GetMappingBlock ( mappingExpressions , mappingDecor ) ;
5452 var mappingBlockWithTryCatch = WrapInTryCatch ( mappingBlock , mapperData ) ;
5553
5654 return mappingBlockWithTryCatch ;
@@ -76,6 +74,20 @@ private bool MappingAlwaysBranchesToDerivedType(IObjectMappingData mappingData,
7674
7775 protected abstract Expression GetDerivedTypeMappings ( IObjectMappingData mappingData ) ;
7876
77+ private static MappingExtras GetMappingExtras ( ObjectMapperData mapperData )
78+ {
79+ var basicMapperData = mapperData . WithNoTargetMember ( ) ;
80+ var preMappingCallback = GetMappingCallbackOrNull ( CallbackPosition . Before , basicMapperData , mapperData ) ;
81+ var postMappingCallback = GetMappingCallbackOrNull ( CallbackPosition . After , basicMapperData , mapperData ) ;
82+ var mapToNullCondition = GetMapToNullConditionOrNull ( mapperData ) ;
83+
84+ return new MappingExtras (
85+ mapperData ,
86+ preMappingCallback ,
87+ postMappingCallback ,
88+ mapToNullCondition ) ;
89+ }
90+
7991 protected static Expression GetMappingCallbackOrNull (
8092 CallbackPosition callbackPosition ,
8193 IBasicMapperData basicData ,
@@ -84,43 +96,61 @@ protected static Expression GetMappingCallbackOrNull(
8496 return mapperData . MapperContext . UserConfigurations . GetCallbackOrNull ( callbackPosition , basicData , mapperData ) ;
8597 }
8698
99+ private static Expression GetMapToNullConditionOrNull ( IMemberMapperData mapperData )
100+ => mapperData . MapperContext . UserConfigurations . GetMapToNullConditionOrNull ( mapperData ) ;
101+
87102 protected abstract IEnumerable < Expression > GetObjectPopulation ( IObjectMappingData mappingData ) ;
88103
89- private Expression GetMappingBlock ( IList < Expression > mappingExpressions , ObjectMapperData mapperData )
104+ private Expression GetMappingBlock ( IList < Expression > mappingExpressions , MappingExtras mappingExtras )
90105 {
106+ Expression returnExpression ;
107+
108+ var mapperData = mappingExtras . MapperData ;
109+
91110 if ( mappingExpressions [ 0 ] . NodeType != ExpressionType . Block )
92111 {
93112 if ( mappingExpressions [ 0 ] . NodeType == ExpressionType . MemberAccess )
94113 {
95- return mappingExpressions [ 0 ] ;
114+ return GetReturnExpression ( mappingExpressions [ 0 ] , mappingExtras ) ;
96115 }
97116
98117 var objectAssignment = mappingExpressions . First ( exp => exp . NodeType == ExpressionType . Assign ) ;
99118
100119 if ( mappingExpressions . Last ( ) == objectAssignment )
101120 {
102121 var assignedValue = ( ( BinaryExpression ) objectAssignment ) . Right ;
122+ returnExpression = GetReturnExpression ( assignedValue , mappingExtras ) ;
103123
104124 if ( mappingExpressions . Count == 1 )
105125 {
106- return assignedValue ;
126+ return returnExpression ;
107127 }
108128
109- mappingExpressions [ mappingExpressions . Count - 1 ] = mapperData . GetReturnLabel ( assignedValue ) ;
129+ mappingExpressions [ mappingExpressions . Count - 1 ] = mapperData . GetReturnLabel ( returnExpression ) ;
110130
111131 return Expression . Block ( mappingExpressions ) ;
112132 }
113133 }
114134
115- var returnValue = GetReturnValue ( mapperData ) ;
135+ returnExpression = GetReturnExpression ( GetReturnValue ( mapperData ) , mappingExtras ) ;
116136
117- mappingExpressions . Add ( mapperData . GetReturnLabel ( returnValue ) ) ;
137+ mappingExpressions . Add ( mapperData . GetReturnLabel ( returnExpression ) ) ;
118138
119139 var mappingBlock = Expression . Block ( new [ ] { mapperData . InstanceVariable } , mappingExpressions ) ;
120140
121141 return mappingBlock ;
122142 }
123143
144+ private static Expression GetReturnExpression ( Expression returnValue , MappingExtras mappingExtras )
145+ {
146+ return ( mappingExtras . MapToNullCondition != null )
147+ ? Expression . Condition (
148+ mappingExtras . MapToNullCondition ,
149+ returnValue . Type . ToDefaultExpression ( ) ,
150+ returnValue )
151+ : returnValue ;
152+ }
153+
124154 protected abstract Expression GetReturnValue ( ObjectMapperData mapperData ) ;
125155
126156 private static Expression WrapInTryCatch ( Expression mappingBlock , IMemberMapperData mapperData )
@@ -182,5 +212,32 @@ private static Expression WrapInTryCatch(Expression mappingBlock, IMemberMapperD
182212 public virtual void Reset ( )
183213 {
184214 }
215+
216+ #region Helper Class
217+
218+ internal class MappingExtras
219+ {
220+ public MappingExtras (
221+ ObjectMapperData mapperData ,
222+ Expression preMappingCallback ,
223+ Expression postMappingCallback ,
224+ Expression mapToNullCondition )
225+ {
226+ MapperData = mapperData ;
227+ PreMappingCallback = preMappingCallback ;
228+ PostMappingCallback = postMappingCallback ;
229+ MapToNullCondition = mapToNullCondition ;
230+ }
231+
232+ public ObjectMapperData MapperData { get ; }
233+
234+ public Expression PreMappingCallback { get ; }
235+
236+ public Expression PostMappingCallback { get ; }
237+
238+ public Expression MapToNullCondition { get ; }
239+ }
240+
241+ #endregion
185242 }
186243}
0 commit comments