Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reversing execution order of Before/AfterMapping #603

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
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