CreateMap<Person, PersonViewModel>()
.ForMember(vm => vm.Yin, opt => opt.MapFrom(src => src.Yang))
.ForMember(vm => vm.SomethingElse, opt => opt.Ignore())
.ReverseMap();
What does this do? Alright, if Person has a getsetter for Address that is of an Address type and the Address type has a City, and what is more PersonViewModel has an AddressCity getsetter well the City property on the Address property on Person is gonna get mapped to AddressCity per the AutoMapper convention right? This is the flattening associated with the tool. Those fields are not called out in the example above but, as .ForAllOtherMembers(opt => opt.Ignore()) is not used, the fields which can be mapped by naming convention will be. The two types in the generic for CreateMap correspond generically to TSource and TDestination IN THAT ORDER so Person is getting mapped to PersonViewModel here right? Well with .ReverseMap() in the mix the one-way mapping becomes a two-way rule as best as I can tell allowing PersonViewModel to be mapped back to Person complete with unflattening! The CreateMap implementations may show up in mass in a method in an implementation of the Profile type in the Automapper namespace and to keep things from being reduantly verbose we have this trick it would seem.
No comments:
Post a Comment