如何使用显式成员映射配置AutoMapper为多态性? [英] How to configure AutoMapper for Polymorphism with explicit member mapping?

查看:154
本文介绍了如何使用显式成员映射配置AutoMapper为多态性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下基本情况:

  Mapper.CreateMap< FromBase,至基站>()
.INCLUDE< FromD1,ToD1>()
&.INCLUDE LT; FromD2,ToD2>();

Mapper.CreateMap< FromD1,ToD1>()
.ForMember(M = GT; m.P0,A => a.MapFrom(X => x.Prop0))
.ForMember(M => m.P1,一个= GT; a.MapFrom(X => x.Prop1));

Mapper.CreateMap< FromD2,ToD2>()
.ForMember(M = GT; m.P0,A => a.MapFrom(X => x.Prop0))
.ForMember(M => m.P2,一个= GT; a.MapFrom(X => x.Prop2));

Mapper.AssertConfigurationIsValid();

FromBase [] 320交织= {
新FromD1(){Prop0 = 10,为prop1 = 11}
新FromD2(){Prop0 = 20,Prop2 = 22}
};

变种TOS = Mapper.Map&下; FromBase [],至基站[]≥(320交织);

使用:

 公共类FromBase {
公众诠释Prop0 {搞定;组; }
}
公共类FromD1:FromBase {
公众诠释为prop1 {搞定;组; }
}
公共类FromD2:FromBase {
公众诠释Prop2 {搞定;组; }
}

公共类至基站{
公众诠释P0 {搞定;组; }
}
公共类ToD1:至基站{
公众诠释P1 {搞定;组; }
}
公共类ToD2:至基站{
公众诠释P2 {搞定;组; }
}



它看起来像的例如的上Automapper的doc。



然而, Mapper.AssertConfigurationIsValid()断言抛出:




AutoMapperConfigurationException道:
按照在
至基站1属性不会映射:P0
添加自定义映射表达式,
忽略,或重命名
FromBase属性




除非我失去了一些东西,与我没有依靠公约,与手工绘制的属性例如,它的唯一的区别 ForMember 。也有几个派生类。
不知道如何将这些会是一个问题,但我想不出别的了。



任何想法?


< DIV CLASS =h2_lin>解决方案

的映射实际上做工精细,虽然配置验证断言失败...


Consider the following basic case:

Mapper.CreateMap<FromBase, ToBase>()
        .Include<FromD1, ToD1>()
        .Include<FromD2, ToD2>();

Mapper.CreateMap<FromD1, ToD1>()
        .ForMember( m => m.P0, a => a.MapFrom( x => x.Prop0 ) )
        .ForMember( m => m.P1, a => a.MapFrom( x => x.Prop1 ) );

Mapper.CreateMap<FromD2, ToD2>()
        .ForMember( m => m.P0, a => a.MapFrom( x => x.Prop0 ) )
        .ForMember( m => m.P2, a => a.MapFrom( x => x.Prop2 ) );

Mapper.AssertConfigurationIsValid();

FromBase[] froms = {
        new FromD1() { Prop0 = 10, Prop1 = 11 },
        new FromD2() { Prop0 = 20, Prop2 = 22 }
};

var tos = Mapper.Map<FromBase[], ToBase[]>( froms );

With:

public class FromBase {
    public int Prop0 { get; set; }
}
public class FromD1 : FromBase {
    public int Prop1 { get; set; }
}
public class FromD2 : FromBase {
    public int Prop2 { get; set; }
}

public class ToBase {
    public int P0 { get; set; }
}
public class ToD1 : ToBase {
    public int P1 { get; set; }
}
public class ToD2 : ToBase {
    public int P2 { get; set; }
}

It looks like the example on Automapper's doc.

However the Mapper.AssertConfigurationIsValid() assertion throws:

AutoMapperConfigurationException: "The following 1 properties on ToBase are not mapped: P0 Add a custom mapping expression, ignore, or rename the property on FromBase."

Unless I am missing something, the only differences with the example it that I am not relying on conventions, mapping the properties manually with ForMember. Also there are several derived classes. Not sure how these would be a problem, but I can't think of anything else.

Any idea?

解决方案

The mapping actually works fine, although the configuration validation assertion fails...

这篇关于如何使用显式成员映射配置AutoMapper为多态性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆