Mapping one source class to multiple derived classes with automapper
Asked Answered
P

3

36

Suppose i have a source class:

public class Source
{
    //Several properties that can be mapped to DerivedBase and its subclasses
}

And some destination classes:

public class DestinationBase
{
     //Several properties
}

public class DestinationDerived1 : DestinationBase
{
     //Several properties
}

public class DestinationDerived2 : DestinationBase
{
     //Several properties
}

Then I wish the derived destination classes to inherit the automapper configuration of the baseclass because I do not want to have to repeat it, is there any way to achieve this?

Mapper.CreateMap<Source, DestinationBase>()
    .ForMember(...)
    // Many more specific configurations that should not have to be repeated for the derived classes
    .ForMember(...);

Mapper.CreateMap<Source, DestinationDerived1 >()
    .ForMember(...);
Mapper.CreateMap<Source, DestinationDerived2 >()
    .ForMember(...);

When I write it like this it does not use the base mappings at all, and include doesn't seem to help me.

Edit: This is what I get:

public class Source
{
    public string Test { get; set; }
    public string Test2 { get; set; }
}

public class DestinationBase
{
    public string Test3 { get; set; }
}

public class DestinationDerived1 : DestinationBase
{
    public string Test4 { get; set; }
}

public class DestinationDerived2 : DestinationBase
{
    public string Test5 { get; set; }
}

Mapper.CreateMap<Source, DestinationBase>()
              .ForMember(d => d.Test3, e => e.MapFrom(s => s.Test))
              .Include<Source, DestinationDerived1>()
              .Include<Source, DestinationDerived2>();

        Mapper.CreateMap<Source, DestinationDerived1>()
              .ForMember(d => d.Test4, e => e.MapFrom(s => s.Test2));

        Mapper.CreateMap<Source, DestinationDerived2>()
              .ForMember(d => d.Test5, e => e.MapFrom(s => s.Test2));

AutoMapper.AutoMapperConfigurationException : Unmapped members were found. Review the types and members below.

Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type

Source -> DestinationDerived1 (Destination member list)

Test3

Palua answered 5/2, 2013 at 10:16 Comment(1)
Thanks but not worked for me. Could you please have a look at Using AutoMapper to map Base Classes question?Gabbey
Y
25

Include derived mappings into base mapping:

Mapper.CreateMap<Source, DestinationBase>()
    .ForMember(d => d.Id, op => op.MapFrom(s => s.Id)) // you can remove this
    .Include<Source, DestinationDerived1>()
    .Include<Source, DestinationDerived2>();

Mapper.CreateMap<Source, DestinationDerived1>()
    .ForMember(d => d.Name, op => op.MapFrom(s => s.Text))
    .ForMember(d => d.Value2, op => op.MapFrom(s => s.Amount));

Mapper.CreateMap<Source, DestinationDerived2>()
    .ForMember(d => d.Value, op => op.MapFrom(s => s.Amount));

Usage:

Mapper.AssertConfigurationIsValid();
var s = new Source() { Id = 2, Amount = 10M, Text = "foo" };
var d1 = Mapper.Map<DestinationDerived1>(s);
var d2 = Mapper.Map<DestinationDerived2>(s);

See Mapping inheritance on AutoMapper wiki.


UPDATE: Here is full code of classes which works as it should.

public class Source
{
    public int Id { get; set; }
    public string Text { get; set; }
    public decimal Amount { get; set; }
}

public class DestinationBase
{
    public int Id { get; set; }
}

public class DestinationDerived1 : DestinationBase
{
    public string Name { get; set; }
    public decimal Value2 { get; set; }
}

public class DestinationDerived2 : DestinationBase
{
    public decimal Value { get; set; }
}

UPDATE (workaround of AutoMapper bug):

public static class Extensions
{
    public static IMappingExpression<Source, TDestination> MapBase<TDestination>(
        this IMappingExpression<Source, TDestination> mapping)
        where TDestination: DestinationBase
    {
        // all base class mappings goes here
        return mapping.ForMember(d => d.Test3, e => e.MapFrom(s => s.Test));
    }
}

And all mappings:

    Mapper.CreateMap<Source, DestinationBase>()
          .Include<Source, DestinationDerived1>()
          .Include<Source, DestinationDerived2>()
          .MapBase();

    Mapper.CreateMap<Source, DestinationDerived1>()
          .MapBase()
          .ForMember(d => d.Test4, e => e.MapFrom(s => s.Test2));

    Mapper.CreateMap<Source, DestinationDerived2>()
          .MapBase()
          .ForMember(d => d.Test5, e => e.MapFrom(s => s.Test2));
Yourself answered 5/2, 2013 at 10:23 Comment(9)
That is actually what I have tried, but it doesn't seem to work, I have a unittest which runs AsserConfigurationIsValid which says that the properties which I map in DestinationBase are not set for the DestinationDerived classes.Glairy
@ErikNordenhök just verified - works fine. Mapper.AssertConfigurationIsValid() says that configuration is validYourself
Couldn't post my code on what i run and the error i get because its too long for a comment, however i can't see anything different from your answer and my example that i edited into the original post however my configuration is not valid.Glairy
@ErikNordenhök why you have posted my answer into your question? Btw reason possibly in mapping same source property to different destination properties. I'll check thatYourself
@ErikNordenhök verified, works just fine for me. I'll update codeYourself
I posted the exact code that i ran, and the error that i get, and that has not change. The reason your example work is that the Id property can be resolved by convention, in my example it cannot find it by convention, and automapper doesn't seem to use the base mapping to resolve it either, and thus fails, change the name of Id in Source to Id2 and see if your example works.Glairy
@ErikNordenhök it does not matter which name has base class property (Id, Foo, Bar) I just put anything that will be common for both derived classesYourself
@ErikNordenhök I've reproduced your issue. I think it's a bug of AutoMapper. The only solution I see with reusing maps is creating extension method which will do mapping for base class. I'll update code in a minuteYourself
@SergeyBerezovskiy Not worked for a similar situation explained on Cannot map from ViewModel to ApplicationUser in AutoMapper 5. Any idea?Gabbey
L
10

For Automapper 8.0.
Current version has new method IncludeAllDerived
Here's working example:

        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Source, DestinationBase>()
                .ForMember(dest => dest.Test3, opt => opt.MapFrom(src => src.Test))
                .IncludeAllDerived();

            cfg.CreateMap<Source, DestinationDerived1>()
                .ForMember(dest => dest.Test4, opt => opt.MapFrom(src => src.Test2));

            cfg.CreateMap<Source, DestinationDerived2>()
                  .ForMember(dest => dest.Test5, opt => opt.MapFrom(src => src.Test2));
        });

        var mapper = config.CreateMapper();

        var source = new Source { Test = "SourceTestProperty", Test2 = "SourceTest2Property" };
        var d1 = mapper.Map<DestinationDerived1>(source);
        var d2 = mapper.Map<DestinationDerived2>(source);

        Assert.Equal("SourceTestProperty", d1.Test3);
        Assert.Equal("SourceTest2Property", d1.Test4);

        Assert.Equal("SourceTestProperty", d2.Test3);
        Assert.Equal("SourceTest2Property", d2.Test5);
Laski answered 18/4, 2019 at 14:5 Comment(0)
R
-1

NB! For those who are having issues with derived interfaces. AutoMapper does not support registering against derived interfaces. Only classes are handled.

To make it work, you have to change your type reference for CreateMap to the class instead of interface.

Example:

interface Interface1 {}
class Class1: Interface1 {}
interface Interface2: Interface1 {}
class Class2: Class1, Interface2 {}

CreateMap<OtherClass, Interface1>().IncludeAllDerived();
CreateMap<OtherClass, Interface2>();

Any mapping against Interface2 will only use the first CreateMap. You will have to identify the second as

CreateMap<OtherClass, Class2>();
Reichard answered 6/9, 2019 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.