From 6fdef6443b3a89f772f98039b252804ad6c47a50 Mon Sep 17 00:00:00 2001 From: Steve Wilkes Date: Tue, 9 May 2017 19:00:57 +0100 Subject: [PATCH] Support for populating nested context Target objects for post-mapping callbacks, re: issue #15 --- .../WhenConfiguringMappingCallbacks.cs | 52 ++++++++++++------- .../MappingExpressionFactoryBase.cs | 6 ++- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/AgileMapper.UnitTests/Configuration/WhenConfiguringMappingCallbacks.cs b/AgileMapper.UnitTests/Configuration/WhenConfiguringMappingCallbacks.cs index a139db0c2..dac3a5dcc 100644 --- a/AgileMapper.UnitTests/Configuration/WhenConfiguringMappingCallbacks.cs +++ b/AgileMapper.UnitTests/Configuration/WhenConfiguringMappingCallbacks.cs @@ -56,8 +56,7 @@ public void ShouldExecuteAPreMappingCallbackForASpecifiedTargetTypeConditionally { using (var mapper = Mapper.CreateNew()) { - mapper - .WhenMapping + mapper.WhenMapping .To() .Before .MappingBegins @@ -87,8 +86,7 @@ public void ShouldExecutePreAndPostMappingCallbacksForASpecifiedMember() var preMappingName = default(string); var postMappingName = default(string); - mapper - .WhenMapping + mapper.WhenMapping .From() .Over() .Before @@ -116,8 +114,7 @@ public void ShouldExecuteAPreMemberMappingCallbackConditionally() { var mappedTargetId = default(Guid); - mapper - .WhenMapping + mapper.WhenMapping .From() .ToANew() .Before @@ -145,8 +142,7 @@ public void ShouldExecuteAPostMemberMappingCallbackConditionally() var mappedAddress = default(Address); var callbackCalled = false; - mapper - .WhenMapping + mapper.WhenMapping .ToANew() .After .Mapping(p => p.Address) @@ -194,8 +190,7 @@ public void ShouldRestrictAPreMappingCallbackByTargetType() { using (var mapper = Mapper.CreateNew()) { - mapper - .WhenMapping + mapper.WhenMapping .To>() .Before .MappingBegins @@ -219,8 +214,7 @@ public void ShouldRestrictAPreMappingCallbackBySourceTypeConditionally() { using (var mapper = Mapper.CreateNew()) { - mapper - .WhenMapping + mapper.WhenMapping .From>() .To>() .Before @@ -245,8 +239,7 @@ public void ShouldExecuteAPostMappingCallbackForASpecifiedTargetTypeConditionall { using (var mapper = Mapper.CreateNew()) { - mapper - .WhenMapping + mapper.WhenMapping .To() .After .MappingEnds @@ -274,8 +267,7 @@ public void ShouldRestrictAPostMappingCallbackByTargetType() { using (var mapper = Mapper.CreateNew()) { - mapper - .WhenMapping + mapper.WhenMapping .To>() .After .MappingEnds @@ -299,8 +291,7 @@ public void ShouldRestrictAPostMappingCallbackBySourceType() { using (var mapper = Mapper.CreateNew()) { - mapper - .WhenMapping + mapper.WhenMapping .From>() .To>() .After @@ -338,5 +329,30 @@ public void ShouldExecuteAPostMappingCallbackForADerivedType() derivedSource.ShouldBeSameAs(source); } } + + // See https://github.com/agileobjects/AgileMapper/issues/15 + [Fact] + public void ShouldPopulateAChildTargetObjectInAPostMappingCallback() + { + using (var mapper = Mapper.CreateNew()) + { + mapper.WhenMapping + .From>() + .To>() + .After + .MappingEnds + .Call(ctx => ctx.Target.Value += "!"); + + var source = new PublicField> + { + Value = new PublicProperty { Value = "Hello" } + }; + + var result = mapper.Map(source).ToANew>>(); + + result.Value.ShouldNotBeNull(); + result.Value.Value.ShouldBe("Hello!"); + } + } } } diff --git a/AgileMapper/ObjectPopulation/MappingExpressionFactoryBase.cs b/AgileMapper/ObjectPopulation/MappingExpressionFactoryBase.cs index d88d0adf8..21482116a 100644 --- a/AgileMapper/ObjectPopulation/MappingExpressionFactoryBase.cs +++ b/AgileMapper/ObjectPopulation/MappingExpressionFactoryBase.cs @@ -42,11 +42,13 @@ public Expression Create(IObjectMappingData mappingData) } var basicMapperData = mapperData.WithNoTargetMember(); + var preMappingCallback = GetMappingCallbackOrNull(CallbackPosition.Before, basicMapperData, mapperData); + var postMappingCallback = GetMappingCallbackOrNull(CallbackPosition.After, basicMapperData, mapperData); mappingExpressions.AddUnlessNullOrEmpty(derivedTypeMappings); - mappingExpressions.AddUnlessNullOrEmpty(GetMappingCallbackOrNull(CallbackPosition.Before, basicMapperData, mapperData)); + mappingExpressions.AddUnlessNullOrEmpty(preMappingCallback); mappingExpressions.AddRange(GetObjectPopulation(mappingData)); - mappingExpressions.AddUnlessNullOrEmpty(GetMappingCallbackOrNull(CallbackPosition.After, basicMapperData, mapperData)); + mappingExpressions.AddUnlessNullOrEmpty(postMappingCallback); var mappingBlock = GetMappingBlock(mappingExpressions, mapperData); var mappingBlockWithTryCatch = WrapInTryCatch(mappingBlock, mapperData);