Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper TwoWay Mapping with same Property Name

Given these two objects

public class UserModel
{
    public string Name {get;set;}
    public IList<RoleModel> Roles {get;set;}
}

public class UserViewModel 
{
    public string Name {get;set;}
    public IList<RoleViewModel> Roles {get;set;} // notice the ViewModel
}

Is this the most optimal way to do the mapping, or is AutoMapper capable of mapping Roles to Roles on its own?

App Config

Mapper.CreateMap<UserModel, UserViewModel>()
    .ForMember(dest => dest.Roles, opt => opt.MapFrom(src => src.Roles));
Mapper.CreateMap<UserViewModel, UserModel>()
    .ForMember(dest => dest.Roles, opt => opt.MapFrom(src => src.Roles));

Implementation

_userRepository.Create(Mapper.Map<UserModel>(someUserViewModelWithRolesAttached);
like image 669
Chase Florell Avatar asked Aug 27 '14 21:08

Chase Florell


2 Answers

You don't need to map the properties. Just make sure that the property names match and there is a mapping defined between them.

Mapper.CreateMap<UserModel, UserViewModel>();
Mapper.CreateMap<UserViewModel, UserModel>();
Mapper.CreateMap<RoleModel, RoleViewModel>();
Mapper.CreateMap<RoleViewModel, RoleModel>();

Or with the cooler way I just found out:

Mapper.CreateMap<UserModel, UserViewModel>().ReverseMap();
Mapper.CreateMap<RoleModel, RoleViewModel>().ReverseMap();
like image 129
Ufuk Hacıoğulları Avatar answered Sep 19 '22 16:09

Ufuk Hacıoğulları


Is this the most optimal way to do the mapping, or is AutoMapper capable of mapping Roles to Roles on its own?

If the property names are identical, you should not have to manually provide a mapping:

Mapper.CreateMap<UserModel, UserViewModel>();
Mapper.CreateMap<UserViewModel, UserModel>();

Just make sure the inner types are mapped as well (RoleViewModelRoleModel)

What this means, however, is that if you change a source or destination property name, AutoMapper mappings can fail silently and cause hard to track down problems (e.g., if you changed UserModel.Roles to UserModel.RolesCollection without changing UserViewModels.Roles).

AutoMapper provides a Mapper.AssertConfigurationIsValid() method that will check all of your mappings for errors and catch misconfigured mappings. It's useful to have a unit test that runs with the build that validates your mappings for this kind of problem.

like image 32
Andrew Whitaker Avatar answered Sep 21 '22 16:09

Andrew Whitaker