UPDATE April 13th, 2018: Automapper 6.1.0 supports unflattening by introducing ReverseMap
. See release notes here
I'm trying to use AutoMapper to unflatten an object.
I have a source as follows
public class Source
{
public string Name {get;set;}
public string Child1Property1 {get;set;}
public string Child1Property2 {get;set;}
public string Child2Property1 {get;set;}
public string Child2Property2 {get;set;}
}
I want to map to this to destination
public class Destination
{
public string Name {get;set;}
public List<Child> Children {get;set;}
}
public class Child
{
public string Property1 {get;set;}
public string Property2 {get;set;}
}
My mapping configuration
public static class AutoMapperConfiguration
{
public static MapperConfiguration Configure()
{
var config = new MapperConfiguration(
cfg =>
{
cfg.CreateMap<Source, Destination>()
.ForMember(dest => dest.Children, /* What do I put here?*/))
// I don't think this is correct
cfg.CreateMap<Source, Child>()
.ForMember(dest => dest.Property1, opt => opt.MapFrom(src => src.Child1Property1))
.ForMember(dest => dest.Property2, opt => opt.MapFrom(src => src.Child1Property2))
.ForMember(dest => dest.Property1, opt => opt.MapFrom(src => src.Child2Property1))
.ForMember(dest => dest.Property2, opt => opt.MapFrom(src => src.Child2Property2));
});
return config;
}
}
Now when I test my code I get using mapper.Map<List<Child>>(source)
I get a AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Which make sense, since there isn't a mapping configured to a List<Child>
. If I do mapper.Map<Child>(source)
, I get a Child
instance with all null
values for the properties.
I'm unfortunately not in a position to modify the Source
class.
Is this possible at all with AutoMapper? and if so how?
ReverseMap
which is supposed to help with unflattening, but I still don't know how it shows this scenario. Could whoever updated it include some sample code that would answer the OP's question? – ahong