Skip to content

Commit

Permalink
Merge pull request MapsterMapper#603 from rafalka/bugfix/602-executio…
Browse files Browse the repository at this point in the history
…n-order-of-before-after-mapping-is-not-correct

Reversing execution order of Before/AfterMapping
  • Loading branch information
andrerav committed Aug 16, 2023
2 parents 3819cd1 + 68a7ea7 commit c49bc32
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
17 changes: 14 additions & 3 deletions src/Mapster.Tests/WhenPerformingAfterMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public void TestCleanup()
[TestMethod]
public void After_Mapping()
{
TypeAdapterConfig<SimplePocoBaseBase, SimpleDto>.NewConfig()
.AfterMapping((src, dest) => dest.Name += "!!!");
TypeAdapterConfig<SimplePocoBase, SimpleDto>.NewConfig()
.AfterMapping((src, dest) => dest.Name += "***");
TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
.AfterMapping((src, dest) => dest.Name += "xxx");

Expand All @@ -27,7 +31,7 @@ public void After_Mapping()
var result = TypeAdapter.Adapt<SimpleDto>(poco);

result.Id.ShouldBe(poco.Id);
result.Name.ShouldBe(poco.Name + "xxx");
result.Name.ShouldBe(poco.Name + "!!!***xxx");
}

[TestMethod]
Expand Down Expand Up @@ -95,11 +99,18 @@ public interface IValidatable
void Validate();
}

public class SimplePoco
public class SimplePocoBaseBase
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public class SimplePocoBase : SimplePocoBaseBase
{
}

public class SimplePoco : SimplePocoBase
{
public Guid Id { get; set; }
}

public class SimpleDto : IValidatable
{
Expand Down
30 changes: 27 additions & 3 deletions src/Mapster.Tests/WhenPerformingBeforeMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ public void TestCleanup()
TypeAdapterConfig.GlobalSettings.Clear();
}

[TestMethod]
public void MapToType_Support_CorrectInheritanceOrder()
{
TypeAdapterConfig<SimplePocoBase, SimpleDto>.NewConfig()
.BeforeMapping((src, result) => result.Type = $"{src.Name}!!!");
TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
.BeforeMapping((src, result) => result.Type += "xxx");

var poco = new SimplePoco
{
Id = Guid.NewGuid(),
Name = "test",
};
var result = TypeAdapter.Adapt<SimpleDto>(poco);
result.Id.ShouldBe(poco.Id);
result.Name.ShouldBe(poco.Name);
result.Type.ShouldBe($"{poco.Name}!!!xxx");
}

[TestMethod]
public void MapToType_Support_Destination_Parameter()
{
Expand All @@ -25,7 +44,7 @@ public void MapToType_Support_Destination_Parameter()
Id = Guid.NewGuid(),
Name = "test",
};

// check expression is successfully compiled
Assert.ThrowsException<NullReferenceException>(() => poco.Adapt<SimpleDto>());
}
Expand Down Expand Up @@ -55,16 +74,21 @@ public void MapToTarget_Support_Destination_Parameter()
result.ShouldBe(new List<int> { 0, 1, 2, 3, });
}

public class SimplePoco
public class SimplePocoBase
{
public Guid Id { get; set; }
public string Name { get; set; }
}

public class SimplePoco : SimplePocoBase
{
public Guid Id { get; set; }
}

public class SimpleDto
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
}
}
}
4 changes: 2 additions & 2 deletions src/Mapster/Adapters/BaseAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,15 @@ protected Expression CreateBlockExpressionBody(Expression source, Expression? de
assignActions.Add(assign);

//before(source, result, destination);
var beforeMappings = arg.Settings.BeforeMappingFactories.Select(it => InvokeMapping(it, source, result, destination, arg, true));
var beforeMappings = arg.Settings.BeforeMappingFactories.Select(it => InvokeMapping(it, source, result, destination, arg, true)).Reverse();
assignActions.AddRange(beforeMappings);

//result.prop = adapt(source.prop);
var mapping = CreateBlockExpression(transformedSource, result, arg);
var settingActions = new List<Expression> {mapping};

//after(source, result, destination);
var afterMappings = arg.Settings.AfterMappingFactories.Select(it => InvokeMapping(it, source, result, destination, arg, false));
var afterMappings = arg.Settings.AfterMappingFactories.Select(it => InvokeMapping(it, source, result, destination, arg, false)).Reverse();
settingActions.AddRange(afterMappings);

//return result;
Expand Down

0 comments on commit c49bc32

Please sign in to comment.