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

栏目: ASP.NET · 发布时间: 6年前

内容简介:http://stackoverflow.com/questions/14705064/mapping-one-source-class-to-multiple-derived-classes-with-automapper

假设我有一个源类:

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:

未映射成员被发现.查看下面的类型和成员.

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

来源 – > DestinationDerived1(目的地成员列表)

TEST3

将派生的映射包含到基本映射中:

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

参见 Mapping inheritance AutoMapper维基.

更新:这是完整的代码,按照它应该工作.

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

和所有映射:

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

http://stackoverflow.com/questions/14705064/mapping-one-source-class-to-multiple-derived-classes-with-automapper


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Java夜未眠

Java夜未眠

蔡学镛 / 电子工业出版社 / 2003-4 / 20.00元

本书是一本散文集。作为一名资深程序设计师,作者走笔清新面独特,简练俏皮的文字下,是作者对工作,对人生的理性思考。书中收录的文章内容贴近程序员的生活,能令读者产生强烈共鸣。此外,书中的部分文章也以轻松的风格剖析了学习Java技术时的常见问题,并以专家眼光和经验推荐介绍了一批优秀的技术书籍,旨在帮助读者兴趣盎然地学习Java。一起来看看 《Java夜未眠》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具