Search code examples
c#asp.net-mvcasp.net-mvc-5automapper-6

AutoMapper to map a child list object


I'm using an generic method to map two classes using Automapper

My generic methods

public class AutoMapperConfiguration
{
    public MapperConfiguration Configure<TSource, TDestination>() where TSource:class where TDestination:class
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.AddProfile<ClientMappingProfile<TSource,TDestination>>();
        });
        return config;
    }
}

ClientMappingProfile.cs

public class ClientMappingProfile<TSource,TDestination>: Profile where TSource : class where TDestination:class 
{
    public ClientMappingProfile()
    {
        CreateMap<TSource, TDestination>().ReverseMap();
    }
}

StudentDetailsViewModel.cs

public class StudentDetailsViewModel
{
    public long ID { get; set; }
    public string FirstName { get; set; }
    public List<QualificationViewModel> listQualificationViewModel { get; set; }
}

QualificationViewModel.cs

public class QualificationViewModel
{
    public long ID { get; set; }
    public long StudentID { get; set; }
    public string ExaminationPassed { get; set; }
}

StudentValueObject.cs

public class StudentValueObject
{
    public long ID { get; set; }
    public string FirstName { get; set; }
    public List<StudentQualificationValueObject> listStudentQualificationValueObject { get; set; }
}

StudentQualificationValueObject.cs

public class StudentQualificationValueObject
{
    public long ID { get; set; }
    public long StudentID { get; set; }
    public string ExaminationPassed { get; set; }
}

Usage

StudentValueObject studentValueObject = new StudentValueObject();
var config = new AutoMapperConfiguration().Configure<StudentValueObject, StudentDetailsViewModel>();
var iMapper = config.CreateMapper();
studentValueObject = iMapper.Map<StudentDetailsViewModel, StudentValueObject>(objStudentModel);

So, this works fine with Mapping StudentDetailsViewModel.cs with StudentValueObject.cs. But it silently fails to copy my child list objects which is List<QualificationViewModel> to List<StudentQualificationValueObject>. The child list object always seems to be null. I'm pretty newbie to AutoMapper. I need some help as to know where am I going wrong or what need to be added/fixed to my generic method, so that the child list object gets copied to with Parent object.

Update -

Currently I'm doing it using below code and its working properly but I'm confused is this the proper way of doing this.

StudentValueObject studentValueObject = new StudentValueObject();
var config = new AutoMapperConfiguration().Configure<StudentValueObject, StudentDetailsViewModel>();
var iMapper = config.CreateMapper();
studentValueObject = iMapper.Map<StudentDetailsViewModel, StudentValueObject>(objStudentModel);
config = new AutoMapperConfiguration().Configure<StudentQualificationValueObject, QualificationViewModel>();
iMapper = config.CreateMapper();
studentValueObject.listStudentQualificationValueObject = iMapper.Map<List<QualificationViewModel>, List<StudentQualificationValueObject>>(objStudentModel.listQualificationViewModel);

Solution

  • You have to map the list properties, cause they have different names in the given parent types and you have to add a mapping for the types used within both lists. Here is an working example for your code:

    public class StudentsMappingProfile : Profile
    {
        public StudentsMappingProfile()
        {
            CreateMap<StudentValueObject, StudentDetailsViewModel>()
                .ForMember(viewModel => viewModel.listQualificationViewModel, conf => conf.MapFrom(value => value.listStudentQualificationValueObject));
            CreateMap<StudentQualificationValueObject, QualificationViewModel>();
        }
    }
    
    public class Program
    {
        public static void Main()
        {
            var config = new MapperConfiguration(cfg => cfg.AddProfile<StudentsMappingProfile>());
            var mapper = config.CreateMapper();
    
            var source = new StudentValueObject { ID = 73, FirstName = "Hello", listStudentQualificationValueObject = new List<StudentQualificationValueObject> { new StudentQualificationValueObject { ID = 42, StudentID = 17, ExaminationPassed = "World" } } };
            var destination = mapper.Map<StudentDetailsViewModel>(source);
    
            Console.ReadKey();
        }
    }