Use AutoMapper to map from an interface to a concrete type

一曲冷凌霜 提交于 2019-11-30 14:05:51

Question 1: Not that I am aware of.

Question 2: When using the .ConstructUsing() make sure you return the mapped object you are after rather than a fresh instance.

e.g.

Mapper.Initialize(config =>
{
    config.CreateMap<IPerson, PersonDto>()
        .Include<IModelPerson, ModelPersonDto>()
        .ConstructUsing((IPerson person) => 
        {
            if (person is IModelPerson) return Mapper.Map<ModelPersonDto>(person);

            throw new InvalidOperationException("Unknown person type: " + person.GetType().FullName);
        })
        ;

    config.CreateMap<IModelPerson, ModelPersonDto>();
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!