EDIT So you can change the implementation of BusinessEntity , you can use AutoMapper agreement authority and anti-aliasing. The business object is modified here:
public class BusinessEntity { public string FirstName { get; set; } // Instead of NameFirst public string LastName { get; set; } public Book Bk { get; set; } // Bk instead of BookDetails public string Salary { get; set; } } public class Book { public string Name { get; set; } // No prefixes public int Price { get; set; } public string Description { get; set; } }
You need the name Bk to resolve Bk.Name to BkName . Now all displays will be generated in several lines:
// For mapping from service entity to book Mapper.Initialize(cfg => cfg.RecognizePrefixes("Bk")); Mapper.CreateMap<BusinessEntity, ServiceEntity>(); // Trick to un-flatten service entity // It is mapped both to Book and BusinessEnity Mapper.CreateMap<ServiceEntity, Book>(); Mapper.CreateMap<ServiceEntity, BusinessEntity>() .ForMember(d => d.Bk, m => m.MapFrom(s => s));
What is it. All 30 properties will be displayed by agreement:
var service = new ServiceEntity { FirstName = "Sergey", LastName = "Berezovskiy", Salary = 5000, BkName = "Laziness in Action", BkDescription = "...", BkPrice = 42 }; var business = Mapper.Map<BusinessEntity>(source); var anotherService = Mapper.Map<ServiceEntity>(business);
ORIGINAL RESPONSE Thus, you use a custom mapping, from the service object to the business object, then the default mapping will also not work for reverse mapping. You must manually provide mappings for members:
Mapper.CreateMap<BusinessEntity, ServiceEntity>() .ForMember(d => d.FirstName, m => m.MapFrom(s => s.Details.NameFirst)) .ForMember(d => d.LastName, m => m.MapFrom(s => s.Details.LastName)) .ForMember(d => d.Salary, m => m.MapFrom(s => s.Details.Salary)) .ForMember(d => d.BkName, m => m.MapFrom(s => s.Details.BookDetails.BookName)) .ForMember(d => d.BkPrice, m => m.MapFrom(s => s.Details.BookDetails.BookPrice)) .ForMember(d => d.BkDescription, m => m.MapFrom(s => s.Details.BookDetails.BookDescription));
But I think that manual matching is better in this case, and then providing all of these cards for individual properties.