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

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

内容简介: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


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

查看所有标签

猜你喜欢:

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

数学世纪

数学世纪

皮耶尔乔治·奥迪弗雷迪 / 胡作玄、胡俊美、于金青 / 上海科学技术出版社 / 2012-1 / 28.00元

《数学世纪:过去100年间30个重大问题》以简短可读的方式论述了整个20世纪的数学。20世纪的数学博大精深,新兴领域及学科的建立发展,许多经典问题得到解决,大量新的有意义的问题的引入,为数学带来了活力。《数学世纪:过去100年间30个重大问题》介绍了数学基础,20世纪的纯粹数学、应用和计算数学,以及目前未解的重要问题,中间穿插了希尔伯特的23个问题的解决情况、菲尔兹奖和沃尔夫奖得主的工作成就等。一起来看看 《数学世纪》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

随机密码生成器
随机密码生成器

多种字符组合密码

SHA 加密
SHA 加密

SHA 加密工具