使用automapper将一个源类映射到多个派生类

时间:2013-02-05 10:16:39

标签: c# automapper automapper-2

假设我有一个源类:

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

一些目的地类:

public class DestinationBase
{
     //Several properties
}

public class DestinationDerived1 : DestinationBase
{
     //Several properties
}

public class DestinationDerived2 : DestinationBase
{
     //Several properties
}

然后我希望派生的目标类继承baseclass的automapper配置,因为我不想重复它,有没有办法实现这个?

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(...);

当我这样写它时根本不使用基本映射,包含似乎对我没有帮助。

编辑: 这就是我得到的:

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: 找到了未映射的成员。查看以下类型和成员。

添加自定义映射表达式,忽略,添加自定义解析程序或修改源/目标类型

来源 - &gt; DestinationDerived1(目标成员列表)

Test3的

3 个答案:

答案 0 :(得分:19)

将派生映射包含在基本映射中:

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));

用法:

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

请参阅AutoMapper wiki上的Mapping inheritance


更新:这是完整的类代码,它可以正常工作。

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; }
}

更新(AutoMapper错误的解决方法):

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));
    }
}

所有映射:

    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));

答案 1 :(得分:0)

对于Automapper 8.0
当前版本具有新方法IncludeAllDerived
这是工作示例:

        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);

答案 2 :(得分:-1)

NB!对于那些派生接口有问题的人。 AutoMapper不支持在派生接口上注册。仅处理类。

要使其工作,必须将CreateMap的类型引用更改为类而不是接口。

示例:

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

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

任何针对Interface2的映射将仅使用第一个CreateMap。您必须将第二个标识为

CreateMap<OtherClass, Class2>();