Created
September 8, 2016 15:17
-
-
Save lbargaoanu/ed96a881399d4b0a8b37c994760541e4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public static void Main() | |
{ | |
Mapper.Initialize(a => a.AddProfile<TestProfile>()); | |
var mapSource = new SourceTestModel { SourceDetailTestProperty = 5, SourceBaseTestProperty = 10, UseFirstModel = true }; | |
var expectedResult = new FirstTargetTestModel { DetailTestProperty = 5, BaseTestProperty = 10 }; | |
Mapper.Map<ITestDetail>(mapSource).Dump(); | |
} | |
public class SourceTestModel | |
{ | |
public int SourceDetailTestProperty { get; set; } | |
public int SourceBaseTestProperty { get; set; } | |
public bool UseFirstModel { get; set; } | |
} | |
public class FirstTargetTestModel : ITestDetail, ITestBase | |
{ | |
public int DetailTestProperty { get; set; } | |
public int BaseTestProperty { get; set; } | |
} | |
public class SecondTargetTestModel : ITestDetail, ITestBase | |
{ | |
public int DetailTestProperty { get; set; } | |
public int BaseTestProperty { get; set; } | |
} | |
public interface ITest { } | |
public interface ITestBase | |
{ | |
int BaseTestProperty { get; set; } | |
} | |
public interface ITestDetail : ITest | |
{ | |
int DetailTestProperty { get; set; } | |
} | |
public class TestProfile : Profile | |
{ | |
public TestProfile() | |
{ | |
RecognizePrefixes("Source"); | |
CreateMap<SourceTestModel, FirstTargetTestModel>(); | |
CreateMap<SourceTestModel, SecondTargetTestModel>(); | |
CreateMap<SourceTestModel, ITestDetail>() | |
.ConvertUsing(a => | |
{ | |
var destination = a.UseFirstModel ? new FirstTargetTestModel() as ITestDetail : new SecondTargetTestModel() as ITestDetail; | |
Mapper.Map(a, destination); | |
return destination; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment