0

I'm new to AutoMapper framework. I have three to Five complex objects which has to be mapped to one object

For example ChipInfo, HardDiskInfo, MonitorInfo, MemoryCardInfo has to be mapped to LaptopInfo because LaptopInfo object has fields that has to be populated from all four objects.

How can this be achievable using AutoMapper. I couldn't find any answers which allows me to do CreateMap using .ForMember to four objects. Please help Thanks

Following is the updated Code

public class AutoMapperConfig
{
   Mapper.Initialize(x =>
    {
         x.AddProfile<chipInfoMapperProfile>();
         x.AddProfile<(hardDiskInfoMapperProfile>();
         x.AddProfile<monitorInfoMapperProfile>();
         x.AddProfile<memoryCardInfoMapperProfile>();
                });
}


public class chipInfoMapperProfile : Profile
    {
        protected override void Configure()
        {
            Mapper.CreateProfile(Profiles.ChipProfileName).CreateMap<chipInfo, laptopInfo>()
                  .ForMember(x => x.LapTopChipProperty, opt => opt.MapFrom(source => source.ChipProperty));
        }
    }

public class hardDiskInfoMapperProfile : Profile
    {
        protected override void Configure()
        {
            Mapper.CreateProfile(Profiles.hardDiskProfileName).CreateMap<hardDiskInfo, laptopInfo>()
                  .ForMember(x => x.LaptopHardDiskProperty, opt => opt.MapFrom(source => source.HardDiskProperty));
        }
    }

public class monitorInfoMapperProfile: Profile
    {
        protected override void Configure()
        {
            Mapper.CreateProfile(Profiles.monitorInfoProfileName).CreateMap<monitorInfo, laptopInfo>()
                  .ForMember(x => x.LaptopMonitorInfoProperty, opt => opt.MapFrom(source => source.MonitorInfoProperty));
        }
    }
AllSpark
  • 425
  • 1
  • 4
  • 17

1 Answers1

1

If you create mappings for each one of those combinations, you can use the overload that lets you map to an existing object. Here's an example.

Your code might look something like this:

var laptopInfo = new LaptopInfo();

Mapper.Map(chipInfo, laptopInfo);
Mapper.Map(hardDiskInfo, laptopInfo);
Mapper.Map(monitorInfo, laptopInfo);
Mapper.Map(memoryCardInfo, laptopInfo);

Essentially, you're just applying a mapping to an existing/destination object for each one of your source objects.

Chris Missal
  • 5,987
  • 3
  • 28
  • 46
  • Thanks for the reply however it isn't working for what i wanna do. I have updated the code in the original question. which gives a errors saying i would have to ignore properties That are not used in each profile. Problem is when i do that it doesn't map everything to one object as a whole. whatever mapped on the last profile only fills the destination object everything else get the default value. – AllSpark Jul 18 '14 at 16:02
  • @ChrisMissal: Would you please be able to have a look at [this question](https://stackoverflow.com/questions/54967828/what-does-automapper-mapatruntime-do) which is about AutoMapper's `MapAtRuntime` function? I am not able to find any documentation on this... – Hooman Bahreini Feb 19 '20 at 01:35