简体   繁体   English

如何配置Automapper 4以允许空目标值

[英]How can I configure Automapper 4 to allow a null destination value

I'm having some problems working out how to get Automapper 4.2.1 to allow for a type mapping where the destination value might be null depending on the source value. 我在解决如何使Automapper 4.2.1允许类型映射时遇到一些问题,其中目标值可能为null,具体取决于源值。

Older versions of Automapper allowed an AllowNullDestination flag to be set via the Mapper configuration but I can't find the equivalent recipe for the new version and the old mechanism of configuring via the static Mapper object seems to have been obsoleted. 较旧版本的Automapper允许通过Mapper配置设置AllowNullDestination标志,但是我找不到新版本的等效配方,并且通过静态Mapper对象配置的旧机制似乎已经过时。

I have tried the following without success: 我试过以下没有成功:

  • Mapper.Configuration.AllowNullDestinationValues = true; Mapper.Configuration.AllowNullDestinationValues = true;
  • Mapper.AllowNullDestinationValues = true; Mapper.AllowNullDestinationValues = true;
  • Mapper.Initialize(c=>c.AllowNullDestinationValues=true); Mapper.Initialize(C => c.AllowNullDestinationValues = TRUE);

Here's a simple test case demonstrating the problem. 这是一个简单的测试案例,展示了这个问题。 This fails on the final line with an AutoMapperMappingException since the Substitute method is returning null. 由于Substitute方法返回null,因此在AutoMapperMappingException的最后一行失败。 I would like both mappings to succeed. 我希望两个映射都能成功。

I would prefer to avoid the use of .ForMember in the solution since in the real scenario I'm trying to address, the mapping between bool and 'object' (actually a custom class) should apply across the entire object tree. 我宁愿避免在解决方案中使用.ForMember ,因为在我试图解决的真实场景中,bool和'object'(实际上是一个自定义类)之间的映射应该应用于整个对象树。

Although there are several similar questions here on StackOverflow, I haven't found one that refers to a recent version of Automapper. 虽然StackOverflow上有几个类似的问题,但我还没有找到一个引用最新版Automapper的问题。

Thanks in advance for any suggestions 在此先感谢您的任何建议

using AutoMapper;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AutoMapperTest
{
    [TestClass]
    public class ExampleTest
    {
        [TestMethod]
        public void NullDestinationCanBeMapped()
        {
            var mapper = new MapperConfiguration(configuration =>
            {
                configuration.CreateMap<Source, Target>();
                //How should the following mapping be modified to pass the test?   
                configuration.CreateMap<bool, object>()
                .Substitute(i => i ? null : new object());
            }).CreateMapper();

            var target1 = mapper.Map<Source, Target>(new Source {Member = false}); //succeeds
            Assert.IsNotNull(target1.Member); //pass
            var target2 = mapper.Map<Source, Target>(new Source {Member = true}); //fails to map with exception
            Assert.IsNull(target2.Member); //not reached
        }
    }

    public class Source
    {
        public bool Member { get; set; }
    }

    public class Target
    {
        public object Member { get; set; }
    }
}

Instead of using Substitute , use ConvertUsing ... 而不是使用替换 ,使用ConvertUsing ...

     configuration.CreateMap<bool, MyClass>()
     .ConvertUsing(i => i ? null : new object());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM