What is the best way to map inner objects with Automapper 2.0
Use the solution in this question (Automapper 1.0)
Create a Custom Value Resolvers
?
public class DTOObject { // MainObject public int Id { get; set; } public string Name { get; set; } // SubObject (TopObject) public string TopText { get; set; } public string TopFont { get; set; } // SubObject (BottomObject) public string BottomText { get; set; } public string BottomFont { get; set; } } public class MainObject { public int Id { get; set; } public string Name { get; set; } public SubObject TopObject { get; set; } public SubObject BottomObject { get; set; } } public class SubObject { public string SubPropText { get; set; } public string SubPropFont { get; set; } }
Custom Value Resolvers
public class CustomResolver : ValueResolver<DTOObject, SubObject>
{
protected override SubObject ResolveCore(DTOObject source)
{
return Mapper.Map<DTOObject, SubObject>(source);
}
}
Mapper.CreateMap<DTOObject, MainObject>().ForMember(d => d.TopObject, mc => mc.MapFrom(s => new SubObject(){ SubPropText = s.TopText, SubPropFont = s.TopFont } ));
– Chintz