AutoMapper - 继承映射不​​工作,同一来源,多个目的地 [英] AutoMapper -- inheritance mapping not working, same source, multiple destinations

查看:315
本文介绍了AutoMapper - 继承映射不​​工作,同一来源,多个目的地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用继承映射AutoMapper(V2.2)具有相同源类型的地图,但不同的目标类型?

Can I use inheritance mapping in AutoMapper (v2.2) for maps with the same Source type but different Destination types?

我有这个基本情况(真正的类有很多属性):

I have this basic situation (the real classes have many more properties):

public abstract class BaseViewModel
{
    public int CommonProperty { get; set;}
}

public class ViewModelA : BaseViewModel
{
    public int PropertyA { get; set; }
}

public class ViewModelB : BaseViewModel
{
    public int PropertyB { get; set; }
}



ViewModelA ViewModelB 是相同的实体类的不同的表示:

ViewModelA and ViewModelB are different representations of the same Entity class:

public class Entity
{
    public int Property1 { get; set; }
    public int Property2 { get; set; }
    public int Property3 { get; set; }
}



我想重复使用相同的映射 BaseViewModel 每个视图模型,如:

Mapper.CreateMap<Entity, BaseViewModel>()
    .Include<Entity, ViewModelA>()
    .Include<Entity, ViewModelB>()
    .ForMember(x => x.CommonProperty, y => y.MapFrom(z => z.Property1));

Mapper.CreateMap<Entity, ViewModelA>()
    .ForMember(x => x.PropertyA, y => y.MapFrom(z => z.Property2));

Mapper.CreateMap<Entity, ViewModelB>()
    .ForMember(x => x.PropertyB, y => y.MapFrom(z => z.Property3));



不过遗憾的是,这似乎并没有工作。呼叫这样的:

But unfortunately, this doesn't seem to work. Calls like these:

var model = Mapper.Map<Entity, ViewModelA>(entity);



导致模式 PropertyA 映射,但并不 CommonProperty 。我相信我按照 https://github.com/AutoMapper/AutoMapper/wiki/Mapping-inheritance例子正确,但恐怕具有相同源类型创建多个地图被绊倒AutoMapper了。

result in model having PropertyA mapped, but not CommonProperty. I believe I'm following the examples in https://github.com/AutoMapper/AutoMapper/wiki/Mapping-inheritance properly, but I'm afraid having multiple maps created with the same Source type is tripping AutoMapper up.

任何见解?我爱分组Base类映射在一起的想法,但是这似乎并没有工作。

Any insights? I love the idea of grouping Base class mappings together, but this doesn't seem to work.

推荐答案

不幸的是在这种情况下, AutoMapper似乎每个注册源类型只有一个子类的映射,最后一个( ViewModelB )。这可能是设计并行的层次,不是一个单一的源类型的工作。

Unfortunately in this case, AutoMapper seems to be registering only one child class mapping per source type, the last one (ViewModelB). This was probably designed to work with parallel hierarchies, not with a single source type.

要解决这个问题,你可以封装在一个扩展方法常见的映射:

To work around this, you can encapsulate the common mappings in an extension method:

public static IMappingExpression<Entity, TDestination> MapBaseViewModel<TDestination>(this IMappingExpression<Entity, TDestination> map)
  where TDestination : BaseViewModel { 
  return map.ForMember(x => x.CommonProperty, y => y.MapFrom(z => z.Property1));
}

和在各个子类的映射使用它:

And use it in the individual subclass mappings:

Mapper.CreateMap<Entity, ViewModelA>()
  .MapBaseViewModel<ViewModelA>()
  .ForMember(x => x.PropertyA, y => y.MapFrom(z => z.Property2));

Mapper.CreateMap<Entity, ViewModelB>()
  .MapBaseViewModel<ViewModelB>()
  .ForMember(x => x.PropertyB, y => y.MapFrom(z => z.Property3));

这篇关于AutoMapper - 继承映射不​​工作,同一来源,多个目的地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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