为什么自动映射器不反转设置“派生所有继承"? [英] Why doesn't automapper reverse the setting "derive all inherited"?

查看:105
本文介绍了为什么自动映射器不反转设置“派生所有继承"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在将域层次结构映射到Dto层次结构,并使用ReverseMap()简化了向域的映射.

We are mapping domain hierarchy to the Dto hierarchy and used ReverseMap() to simplify mapping back to domain.

将所有派生对象都包括到映射中是非常烦人的.这就是为什么我们尝试使用IncludeAllDerived()的原因.一段时间以来,效果确实很好,但是过了一会儿,我们出现了奇怪的异常:

Including all the individual derivates into the mapping was pretty annoying. That's why we've tried to use IncludeAllDerived(). That did work good for some time, but after a while we've got strange exceptions:

System.ArgumentException : Cannot create an instance of abstract type Xxx.Base

经过一些调查,我们发现这是由于使用了IncludeAllDerived().当我们将其更改为显式包含时,它又可以正常工作了.

After some investigations we've found out, that it was due to using the IncludeAllDerived(). As we've changed it to the explicit includes, it was working again.

我们问自己的问题是是IncludeAllDerived或ReverseMap还是Automapper无法处理抽象基类型或其他任何东西".

The question we were asking ourself was "is it a IncludeAllDerived or ReverseMap or cann't Automapper handle abstract base types or whatever".

推荐答案

一些进一步的研究表明,这是IncludeAllDerived和ReverseMap的组合,这是一个问题.如果我们在ReverseMap之后重复IncludeAllDerived,则它将按预期工作.令人困惑的是,不需要在ReverseMap之后重复包含调用.

Some further investigations have shown, that it was a combination of IncludeAllDerived and ReverseMap that was an issue. If we repeat IncludeAllDerived after ReverseMap, than it works as expected. The confusing part is, that repeating Include-calls after ReverseMap was not required.

以下是用于复制的代码(Automapper 9.0.0):

Here is the code for reproduction (Automapper 9.0.0):

    public abstract class Base { }
    public class Derived: Base { }

    public abstract class BaseDto { }
    public class DerivedDto: BaseDto { }

    [Test]
    public void MappingBase_WithReverseMap_AllDerived()
    {
        var configuration = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Base, BaseDto>()
                .IncludeAllDerived()
                .ReverseMap()
                .IncludeAllDerived(); // doesn't work without repeating IncludeAllDerived() after ReverseMap()

            cfg.CreateMap<Derived, DerivedDto>()
                .ReverseMap();
        });

        var mapper = configuration.CreateMapper();

        BaseDto derivedDto = new DerivedDto();
        var vm = mapper.Map<Base>(derivedDto);

        vm.Should().NotBeNull();
        vm.Should().BeOfType<Derived>();
    }

    [Test]
    public void MappingBase_WithReverseMap_IncludeBase()
    {
        var configuration = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Base, BaseDto>()
                .Include<Derived, DerivedDto>()
                .ReverseMap(); // works without repeating Include() after ReverseMap()

            cfg.CreateMap<Derived, DerivedDto>()
                .ReverseMap();
        });

        var mapper = configuration.CreateMapper();

        BaseDto derivedDto = new DerivedDto();
        var vm = mapper.Map<Base>(derivedDto);

        vm.Should().NotBeNull();
        vm.Should().BeOfType<Derived>();
    }

这篇关于为什么自动映射器不反转设置“派生所有继承"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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