0

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);
Kumar
  • 303
  • 6
  • 17
  • Your configuration needs to contain a defintion on how to map from `StudentQualificationValueObject` to `QualificationViewModel`. Otherwise it doesn't know how to convert these types, but you don't need to define the conversion for the collection, just for the types itself. – Oliver Jun 18 '18 at 06:12
  • Please see my update. – Kumar Jun 18 '18 at 07:02

1 Answers1

0

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();
    }
}
Oliver
  • 43,366
  • 8
  • 94
  • 151
  • Thanks Oliver. Its working now. Can we make this more generic that if we pass the class as parameter it should automatically map the child objects. Also, this handles the null objects . – Kumar Jun 19 '18 at 15:09
  • I also tried this generic approach in the beginning. Unfortunately it doesn't work well, cause you have almost always some special handling somewhere in your object tree mapping. IMHO it works better to create profiles (like I did in my answer) and create the mapper from the different profiles. – Oliver Jun 20 '18 at 09:37
  • Yes facing the same issue while making it more generic. So had to go with creating different profiles approach. – Kumar Jun 20 '18 at 15:12