自动映射器的条件被忽略 [英] Automapper's condition gets ignored

查看:70
本文介绍了自动映射器的条件被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题 似乎情况被忽略了.这是我的情况:

Issue Seems like the condition gets ignored. Here is my scenario:

源类

public class Source
{
    public IEnumerable<Enum1> Prop1{ get; set; }

    public IEnumerable<Enum2> Prop2{ get; set; }

    public IEnumerable<Enum3> Prop3{ get; set; }
}

一个字节的enums子类,并用[Flags]装饰.目标类仅包含诸如Enum1,Enum2和Enum3之类的属性,其中包含总计"按位值.因此,从本质上讲,如果Enumeration包含Enum1.value!,Enum1.Value2和Enum1.Value3,则目标将包含Enum1.Value1的按位值| Enum1.Value2 | Enum1.Value3

The enums subclass from a byte and are decorated with [Flags]. The destination class simply contains properties like Enum1, Enum2 and Enum3 containging the "total" bitwise value. So in essense if the Enumeration contains Enum1.value!, Enum1.Value2 and Enum1.Value3, the destination will contain the bitwise value of Enum1.Value1 | Enum1.Value2 | Enum1.Value3

目的地类别

    public Enum1 Prop1 { get; set; }

    public Enum2 Prop2 { get; set; }

    public Enum3 Prop3 { get; set; }

AutoMapper映射

    Mapper.CreateMap<Source, Destination>()
            .ForMember(m => m.Prop1, o =>
                {
                    o.Condition(c => !c.IsSourceValueNull);
                    o.MapFrom(f => f.Prop1.Aggregate((current, next) => current | next));
                })
            .ForMember(m => m.Prop2, o =>
            {
                o.Condition(c => !c.IsSourceValueNull);
                o.MapFrom(f => f.Prop2.Aggregate((current, next) => current | next));
            })
            .ForMember(m => m.Prop3, o =>
            {
                o.Condition(c => !c.IsSourceValueNull);
                o.MapFrom(f => f.Prop3.Aggregate((current, next) => current | next));
            });  

当内部属性不为null且映射成功并正确设置目标时,映射工作正常.但是,我想在成员源值为null时跳过映射(当Prop1为null时,则跳过映射).

The mapping works fine when the inner properties are not null and mapping succeeds and sets destination correctly. However, I want to skip the mapping when the member source value is null (when Prop1 is null, then skip the mapping).

从调试中我可以看到Source.Prop1为null.该条件将被完全忽略,并得到异常,表明该值为null.

I can see from debugging that Source.Prop1 is null. The condition gets completely ignored and get the exception saying the value is null.

Trying to map Source to Destination. Destination property: Prop1. Exception of type 'AutoMapper.AutoMapperMappingException' was thrown. --> Value cannot be null. Parameter name: source

我不确定IsSourceValueNull是否检查Prop1或不为null的实际Source类.只有成员Prop1为空.

I'm not sure if IsSourceValueNull checking for Prop1 or the the actual Source class which is not null. Only the member Prop1 is null.

感谢任何帮助.

推荐答案

我认为您需要分两个步骤进行ConditionMapFrom:

I think you need to do the Condition and MapFrom in two steps:

.ForMember(c => c.Prop1, o => o.Condition(c => !c.IsSourceValueNull));
.ForMember(c => c.Prop1, o => o.MapFrom(f => f.Prop1.Aggregate(...));

如果条件评估为false,则将永远不会使用MapFrom.

The MapFrom will never be used if the Condition evaluates to false.

编辑

嗯...这似乎不起作用.我以为我以前在某处使用过它.您可以只使用MapFrom:

Hmmm... That doesn't seem to work. I thought I had used that before somewhere. You could resort to just the MapFrom:

.MapFrom(f => f.Prop1 == null ? null : f.Prop1.Aggregate(...));

这篇关于自动映射器的条件被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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