Created
July 5, 2016 15:10
-
-
Save mcliment/39882d316e5a57e07af7dd9ef289d671 to your computer and use it in GitHub Desktop.
Just showing an Automapper 5.0 inconsistency
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
using System; | |
using AutoMapper; | |
namespace SMBugSample | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var workingConfig = new MapperConfiguration(cfg => | |
{ | |
cfg.CreateMap<SimpleSrc, SimpleDst>() | |
.ForMember(m => m.Value, o => o.ResolveUsing(s => new Bar { Value = s.Value.Value })); | |
cfg.CreateMap<ComplexSrc, ComplexDst>() | |
.IncludeBase<SimpleSrc, SimpleDst>() | |
.ForMember(m => m.OtherValue, o => o.ResolveUsing(s => new Bar { Value = s.OtherValue.Value })); | |
}); | |
var failingConfig = new MapperConfiguration(cfg => | |
{ | |
cfg.CreateMap<SimpleSrc, SimpleDst>() | |
.ForMember(m => m.Value, o => o.ResolveUsing<Resolver>()); | |
cfg.CreateMap<ComplexSrc, ComplexDst>() | |
.IncludeBase<SimpleSrc, SimpleDst>() | |
//.ForMember(m => m.Value, o => o.ResolveUsing<Resolver>()) | |
.ForMember(m => m.OtherValue, o => o.ResolveUsing(s => new Bar { Value = s.OtherValue.Value })); | |
}); | |
workingConfig.AssertConfigurationIsValid(); | |
failingConfig.AssertConfigurationIsValid(); | |
var mapper = workingConfig.CreateMapper(); | |
var simple = mapper.Map<SimpleDst>(new SimpleSrc { Value = new Foo { Value = "Forty two" } }); | |
var complex = mapper.Map<ComplexDst>(new ComplexSrc { Value = new Foo { Value = "Forty two" }, OtherValue = new Foo { Value = "Ninety nine" } }); | |
Console.WriteLine("Simple: " + simple.Value.Value); | |
Console.WriteLine("Complex: " + complex.Value.Value + " " + complex.OtherValue.Value); | |
Console.ReadKey(); | |
} | |
} | |
public class Resolver : IValueResolver<SimpleSrc, Bar> | |
{ | |
public Bar Resolve(SimpleSrc source, Bar destination, ResolutionContext context) | |
{ | |
return new Bar { Value = source.Value.Value }; | |
} | |
} | |
public class Foo | |
{ | |
public string Value { get; set; } | |
} | |
public class Bar | |
{ | |
public string Value { get; set; } | |
} | |
public class SimpleSrc | |
{ | |
public Foo Value { get; set; } | |
} | |
public class ComplexSrc : SimpleSrc | |
{ | |
public Foo OtherValue { get; set; } | |
} | |
public class SimpleDst | |
{ | |
public Bar Value { get; set; } | |
} | |
public class ComplexDst : SimpleDst | |
{ | |
public Bar OtherValue { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment