From 68a7ea7ec8125d5298e9ee99c000a955b106677f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Kukla?= Date: Tue, 4 Jul 2023 11:19:06 +0200 Subject: [PATCH] Reversing execution order of Before/AfterMapping --- .../WhenPerformingAfterMapping.cs | 17 +++++++++-- .../WhenPerformingBeforeMapping.cs | 30 +++++++++++++++++-- src/Mapster/Adapters/BaseAdapter.cs | 4 +-- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/Mapster.Tests/WhenPerformingAfterMapping.cs b/src/Mapster.Tests/WhenPerformingAfterMapping.cs index b2c3f94f..68b06a62 100644 --- a/src/Mapster.Tests/WhenPerformingAfterMapping.cs +++ b/src/Mapster.Tests/WhenPerformingAfterMapping.cs @@ -16,6 +16,10 @@ public void TestCleanup() [TestMethod] public void After_Mapping() { + TypeAdapterConfig.NewConfig() + .AfterMapping((src, dest) => dest.Name += "!!!"); + TypeAdapterConfig.NewConfig() + .AfterMapping((src, dest) => dest.Name += "***"); TypeAdapterConfig.NewConfig() .AfterMapping((src, dest) => dest.Name += "xxx"); @@ -27,7 +31,7 @@ public void After_Mapping() var result = TypeAdapter.Adapt(poco); result.Id.ShouldBe(poco.Id); - result.Name.ShouldBe(poco.Name + "xxx"); + result.Name.ShouldBe(poco.Name + "!!!***xxx"); } [TestMethod] @@ -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 { diff --git a/src/Mapster.Tests/WhenPerformingBeforeMapping.cs b/src/Mapster.Tests/WhenPerformingBeforeMapping.cs index d47f5f4d..5c000802 100644 --- a/src/Mapster.Tests/WhenPerformingBeforeMapping.cs +++ b/src/Mapster.Tests/WhenPerformingBeforeMapping.cs @@ -14,6 +14,25 @@ public void TestCleanup() TypeAdapterConfig.GlobalSettings.Clear(); } + [TestMethod] + public void MapToType_Support_CorrectInheritanceOrder() + { + TypeAdapterConfig.NewConfig() + .BeforeMapping((src, result) => result.Type = $"{src.Name}!!!"); + TypeAdapterConfig.NewConfig() + .BeforeMapping((src, result) => result.Type += "xxx"); + + var poco = new SimplePoco + { + Id = Guid.NewGuid(), + Name = "test", + }; + var result = TypeAdapter.Adapt(poco); + result.Id.ShouldBe(poco.Id); + result.Name.ShouldBe(poco.Name); + result.Type.ShouldBe($"{poco.Name}!!!xxx"); + } + [TestMethod] public void MapToType_Support_Destination_Parameter() { @@ -25,7 +44,7 @@ public void MapToType_Support_Destination_Parameter() Id = Guid.NewGuid(), Name = "test", }; - + // check expression is successfully compiled Assert.ThrowsException(() => poco.Adapt()); } @@ -55,16 +74,21 @@ public void MapToTarget_Support_Destination_Parameter() result.ShouldBe(new List { 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; } } } } diff --git a/src/Mapster/Adapters/BaseAdapter.cs b/src/Mapster/Adapters/BaseAdapter.cs index 0508808b..cd7e0887 100644 --- a/src/Mapster/Adapters/BaseAdapter.cs +++ b/src/Mapster/Adapters/BaseAdapter.cs @@ -238,7 +238,7 @@ 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); @@ -246,7 +246,7 @@ protected Expression CreateBlockExpressionBody(Expression source, Expression? de var settingActions = new List {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;