forked from MapsterMapper/Mapster
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request MapsterMapper#54 from chaowlert/after-mapping
After mapping
- Loading branch information
Showing
9 changed files
with
281 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using NUnit.Framework; | ||
using Should; | ||
|
||
namespace Mapster.Tests | ||
{ | ||
[TestFixture] | ||
public class WhenPerformingAfterMapping | ||
{ | ||
[TearDown] | ||
public void TearDown() | ||
{ | ||
TypeAdapterConfig.GlobalSettings.Clear(); | ||
} | ||
|
||
[Test] | ||
public void After_Mapping() | ||
{ | ||
TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig() | ||
.AfterMapping((src, dest) => dest.Name += "xxx"); | ||
|
||
var poco = new SimplePoco | ||
{ | ||
Id = Guid.NewGuid(), | ||
Name = "test", | ||
}; | ||
var result = TypeAdapter.Adapt<SimpleDto>(poco); | ||
|
||
result.Id.ShouldEqual(poco.Id); | ||
result.Name.ShouldEqual(poco.Name + "xxx"); | ||
} | ||
|
||
[Test] | ||
public void After_Mapping_With_DestinationType_Setting() | ||
{ | ||
TypeAdapterConfig.GlobalSettings.ForDestinationType<IValidatable>() | ||
.AfterMapping(dest => dest.Validate()); | ||
|
||
var poco = new SimplePoco | ||
{ | ||
Id = Guid.NewGuid(), | ||
Name = "test", | ||
}; | ||
var result = TypeAdapter.Adapt<SimpleDto>(poco); | ||
|
||
result.IsValidated.ShouldBeTrue(); | ||
} | ||
|
||
public interface IValidatable | ||
{ | ||
void Validate(); | ||
} | ||
|
||
public class SimplePoco | ||
{ | ||
public Guid Id { get; set; } | ||
public string Name { get; set; } | ||
} | ||
|
||
public class SimpleDto : IValidatable | ||
{ | ||
public Guid Id { get; set; } | ||
public string Name { get; set; } | ||
|
||
public bool IsValidated { get; private set; } | ||
|
||
public void Validate() | ||
{ | ||
this.IsValidated = true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
|
||
namespace Mapster.Adapters | ||
{ | ||
public abstract class BaseAfterMapper | ||
{ | ||
protected virtual int Score => 0; | ||
|
||
public virtual int? Priority(Type sourceType, Type destinationType, MapType mapType) | ||
{ | ||
return CanMap(sourceType, destinationType, mapType) ? this.Score : (int?)null; | ||
} | ||
|
||
protected abstract bool CanMap(Type sourceType, Type destinationType, MapType mapType); | ||
|
||
public LambdaExpression CreateAfterMapFunc(CompileArgument arg) | ||
{ | ||
var p = Expression.Parameter(arg.SourceType); | ||
var p2 = Expression.Parameter(arg.DestinationType); | ||
var body = CreateExpressionBody(p, p2, arg); | ||
return Expression.Lambda(body, p, p2); | ||
} | ||
|
||
protected abstract Expression CreateExpressionBody(Expression source, Expression destination, CompileArgument arg); | ||
|
||
public TypeAdapterRule CreateRule() | ||
{ | ||
var settings = new TypeAdapterSettings(); | ||
settings.AfterMappingFactories.Add(this.CreateAfterMapFunc); | ||
var rule = new TypeAdapterRule | ||
{ | ||
Priority = this.Priority, | ||
Settings = settings, | ||
}; | ||
DecorateRule(rule); | ||
return rule; | ||
} | ||
|
||
protected virtual void DecorateRule(TypeAdapterRule rule) { } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.