Search code examples
c#automapper-3

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);

Solution

  • 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.