Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public void ShouldExecuteAPreMappingCallbackForASpecifiedTargetTypeConditionally
{
using (var mapper = Mapper.CreateNew())
{
mapper
.WhenMapping
mapper.WhenMapping
.To<Person>()
.Before
.MappingBegins
Expand Down Expand Up @@ -87,8 +86,7 @@ public void ShouldExecutePreAndPostMappingCallbacksForASpecifiedMember()
var preMappingName = default(string);
var postMappingName = default(string);

mapper
.WhenMapping
mapper.WhenMapping
.From<Person>()
.Over<PersonViewModel>()
.Before
Expand Down Expand Up @@ -116,8 +114,7 @@ public void ShouldExecuteAPreMemberMappingCallbackConditionally()
{
var mappedTargetId = default(Guid);

mapper
.WhenMapping
mapper.WhenMapping
.From<Person>()
.ToANew<Person>()
.Before
Expand Down Expand Up @@ -145,8 +142,7 @@ public void ShouldExecuteAPostMemberMappingCallbackConditionally()
var mappedAddress = default(Address);
var callbackCalled = false;

mapper
.WhenMapping
mapper.WhenMapping
.ToANew<Person>()
.After
.Mapping(p => p.Address)
Expand Down Expand Up @@ -194,8 +190,7 @@ public void ShouldRestrictAPreMappingCallbackByTargetType()
{
using (var mapper = Mapper.CreateNew())
{
mapper
.WhenMapping
mapper.WhenMapping
.To<PublicField<string>>()
.Before
.MappingBegins
Expand All @@ -219,8 +214,7 @@ public void ShouldRestrictAPreMappingCallbackBySourceTypeConditionally()
{
using (var mapper = Mapper.CreateNew())
{
mapper
.WhenMapping
mapper.WhenMapping
.From<PublicProperty<string>>()
.To<PublicField<string>>()
.Before
Expand All @@ -245,8 +239,7 @@ public void ShouldExecuteAPostMappingCallbackForASpecifiedTargetTypeConditionall
{
using (var mapper = Mapper.CreateNew())
{
mapper
.WhenMapping
mapper.WhenMapping
.To<PersonViewModel>()
.After
.MappingEnds
Expand Down Expand Up @@ -274,8 +267,7 @@ public void ShouldRestrictAPostMappingCallbackByTargetType()
{
using (var mapper = Mapper.CreateNew())
{
mapper
.WhenMapping
mapper.WhenMapping
.To<PublicProperty<string>>()
.After
.MappingEnds
Expand All @@ -299,8 +291,7 @@ public void ShouldRestrictAPostMappingCallbackBySourceType()
{
using (var mapper = Mapper.CreateNew())
{
mapper
.WhenMapping
mapper.WhenMapping
.From<PublicField<string>>()
.To<PublicProperty<string>>()
.After
Expand Down Expand Up @@ -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<PublicProperty<string>>()
.To<PublicField<string>>()
.After
.MappingEnds
.Call(ctx => ctx.Target.Value += "!");

var source = new PublicField<PublicProperty<string>>
{
Value = new PublicProperty<string> { Value = "Hello" }
};

var result = mapper.Map(source).ToANew<PublicProperty<PublicField<string>>>();

result.Value.ShouldNotBeNull();
result.Value.Value.ShouldBe("Hello!");
}
}
}
}
6 changes: 4 additions & 2 deletions AgileMapper/ObjectPopulation/MappingExpressionFactoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down