Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper not working with collection of derived classes

I'm facing a problem with AutoMapper 2.1.267.0 when working with objects containing collections of derived classes. I've isolated my problem in a simpler scenario with the following classes:

public class ClassABase
{
    public string Name { get; set; }
}

public class ClassA : ClassABase
{
    public string Description { get; set; }
}

public class ClassBBase
{
    public string Title { get; set; }
}

public class ClassB : ClassBBase
{
    public string Text { get; set; }
}

public class ContainerA
{
    public IList<ClassA> ClassAList { get; set; }
    public ClassA ClassA { get; set; }
}

public class ContainerB
{
    public IList<ClassB> ClassBList { get; set; }
    public ClassB ClassB { get; set; }
}

and these mappings

public class ClassABaseToClassBBase : Profile
{
    protected override void Configure()
    {
        CreateMap<ClassABase, ClassBBase>()
            .Include<ClassA, ClassB>()
            .ForMember(dest => dest.Title, opt => opt.MapFrom(src => src.Name));

        Mapper.AssertConfigurationIsValid();
    }
}

public class ClassAToClassB : Profile
{
    protected override void Configure()
    {
        CreateMap<ClassA, ClassB>()
            .ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.Description));

        Mapper.AssertConfigurationIsValid();
    }
}

public class ContainerAToContainerB : Profile
{
    protected override void Configure()
    {
        CreateMap<ContainerA, ContainerB>()
            .ForMember(dest => dest.ClassBList,
                       opt => opt.MapFrom(src => Mapper.Map<IList<ClassA>, IList<ClassB>>(src.ClassAList)))
            .ForMember(dest => dest.ClassB, opt => opt.MapFrom(src => src.ClassA));

    }
}

Here is the initialization

Mapper.Initialize(x =>
    {
        x.AddProfile<ClassABaseToClassBBase>();
        x.AddProfile<ClassAToClassB>();
        x.AddProfile<ContainerAToContainerB>();
    });

and a diagram summarizing everything (Red arrows represent AutoMapper mappings)

enter image description here

I've mapped a ContainerA instance to ContainerB. This first scenario works properly. The destination object is fully filled as shown in the image bellow:

enter image description here


But if I add this mapping

public class ClassBBaseToClassB : Profile
{
    protected override void Configure()
    {
        CreateMap<ClassBBase, ClassB>()
            .ForMember(dest => dest.Text, opt => opt.Ignore());

        Mapper.AssertConfigurationIsValid();
    }
}

Mapper.Initialize(x =>
    {
        x.AddProfile<ClassABaseToClassBBase>();
        x.AddProfile<ClassAToClassB>();
        x.AddProfile<ClassBBaseToClassB>();
        x.AddProfile<ContainerAToContainerB>();
    });

enter image description here

the result is

enter image description here

The "Text" properties of all elements of the collection become null. As you can see in the image, the "Text" property of the containerB.ClassB object is still mapped correctly. This only occurs with collections. I don't know what's happening. Any clues?

Thanks

like image 365
peflorencio Avatar asked Sep 18 '12 18:09

peflorencio


People also ask

When should you not use AutoMapper?

If you have to do complex mapping behavior, it might be better to avoid using AutoMapper for that scenario. Reverse mapping can get very complicated very quickly, and unless it's very simple, you can have business logic showing up in mapping configuration.

What can I use instead of AutoMapper?

AutoMapper is one of the popular object-object mapping libraries with over 296 million NuGet package downloads. It was first published in 2011 and its usage is growing ever since. Mapster is an emerging alternative to AutoMapper which was first published in 2015 and has over 7.4 million NuGet package downloads.

How does AutoMapper work internally?

How AutoMapper works? AutoMapper internally uses a great concept of programming called Reflection. Reflection in C# is used to retrieve metadata on types at runtime. With the help of Reflection, we can dynamically get a type of existing objects and invoke its methods or access its fields and properties.

How does AutoMapper work in C#?

AutoMapper in C# is a library used to map data from one object to another. It acts as a mapper between two objects and transforms one object type into another. It converts the input object of one type to the output object of another type until the latter type follows or maintains the conventions of AutoMapper.


1 Answers

Hi,

Try to change the mapping between containers in this way :

  public class ContainerAToContainerB : Profile
    {
        protected override void Configure()
        {
            CreateMap<ContainerA, ContainerB>()
                .ForMember(dest => dest.ClassBList,opt=>opt.MapFrom(src=>src.ClassAList))
                .ForMember(dest => dest.ClassB, opt => opt.MapFrom(src => src.ClassA));

        }
}
like image 87
Cybermaxs Avatar answered Sep 19 '22 16:09

Cybermaxs