繁体   English   中英

自动映射器:使用可为空类型的意外映射行为

[英]Automapper: Unexpected mapping behavior using nullable type

使用自动映射器(6.6.2),我试图将可为空的布尔值映射到目标对象属性(dest.IsEnabled.Value)。 但是,只要源值是null,整个目标属性(dest.IsEnabled)就会为null。 但是我只期望属性“值”为空。

知道怎么做对吗?

class Program
{
    static void Main(string[] args)
    {
        var source = new Source();
        source.IsEnabled = null;

        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Source, Dest>();
                //.ConstructUsing(ctor => new Dest());
            cfg.CreateMap<bool?, IsEnabledProperty>()
                .ForMember(dst => dst.Value, opt => opt.MapFrom(src => src));
        });

        //var debug = config.BuildExecutionPlan(typeof(Source), typeof(Dest));

        var mapper = config.CreateMapper();

        //var dest = mapper.Map<Source, Dest>(source, new Dest());
        var dest = mapper.Map<Source, Dest>(source);

        if (dest.IsEnabled == null)
        {
            Console.WriteLine("IsEnabled is null. But why? I expect IsEnabled.Value to be null.");
        }

        Console.ReadLine();
    }
}

class Source
{
    public bool? IsEnabled { get; set; }
}

class Dest
{
    public IsEnabledProperty IsEnabled { get; set; } 
        = new IsEnabledProperty() { IsRequired = true };

    // Just to check if the property is initialized
    public OtherProperty OtherProperty { get; set; }
        = new OtherProperty() { IsRequired = true };
}

class IsEnabledProperty
{
    public bool IsRequired { get; set; }
    public bool? Value { get; set; }
}

class OtherProperty
{
    public bool IsRequired { get; set; }
    public bool? Value { get; set; }
}

更新资料

当我像这样更新映射时

cfg.CreateMap<bool?, IsEnabledProperty>()
   .ConvertUsing((src, dst, context) => 
   {
      if (dst != null) 
         dst.Value = src;
      return dst;
   });

dest.IsEnabled.Value可以正确映射所有变体(null,false,true),也可以正确映射属性destination.IsEnabled.IsRequired的初始化值为true, 但是它迫使我将destination的实例传递给映射方法var dest = mapper.Map<Source, Dest>(source, new Dest()); 在我眼里这没有任何意义,因为我希望目标对象应该由自动映射器以相同的方式构造?

在查看执行计划时,我问我,为什么它在第11行中检查(dest == null) ,而不仅使用初始化的typeMapDestination

(src, dest, ctxt) =>
{
    Dest typeMapDestination;
    return (src == null)
        ? null
        : {
            typeMapDestination = dest ?? new Dest();
            try
            {
                var resolvedValue = ((src == null) || false) ? null : src.IsEnabled;
                var propertyValue = mappingFunction.Invoke(resolvedValue, (dest == null) ? null : typeMapDestination.IsEnabled, ctxt);
                typeMapDestination.IsEnabled = propertyValue;
            }
            catch (Exception ex)
            {
                throw new AutoMapperMappingException(
                    "Error mapping types.",
                    ex,
                    AutoMapper.TypePair,
                    TypeMap,
                    PropertyMap);
            };

            return typeMapDestination;
        };
}

我想您想绘制整个bool? 而不只是值dest.IsEnabled.Value 这样,您可以询问布尔值是否具有dest.HasValue的值,并使用具有dest.Value的值。

您可能需要将AllowNullableMapping添加到AllowNullableMapping配置中。

暂无
暂无

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

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