属性类型被忽略时Automapper的奇怪行为 [英] Strange behavior of Automapper when property type is ignored

查看:110
本文介绍了属性类型被忽略时Automapper的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到

After I saw how to ignore a property type with Automapper, I have tried it in a test project. It turns out that the property of a specific type is ignored properly, but when invoking AssertConfigurationIsValid() an exception is thrown, specifying that unmapped members were found. I can understand the reasoning of this exception, since the members of the type which should be ignored are not mapped, but what I am wondering is if this exceptions should be thrown in the context where I removed a mapping on purpose.

对于给定的代码:

class Type1
{
    public int Prop1 { get; set; }
    public string Prop2 { get; set; }
    public string Prop3 { get; set; }
}

class Type2
{
    public int Prop1 { get; set; }
    public string Prop2 { get; set; }
    public TypeToIgnore Prop3 { get; set; }
}

class MappingProfile : Profile
{
    public MappingProfile()
    {
        ShouldMapProperty = p => p.PropertyType != typeof(TypeToIgnore);
        CreateMap<Type2, Type1>();
    }
}

//...

var config = new MapperConfiguration(cfg => cfg.AddProfile(new MappingProfile()));
config.AssertConfigurationIsValid(); //this throws AutoMapperConfigurationException

在验证配置的有效性时,例如忽略属性本身的情况下,忽略成员而不抛出异常不是Automapper的正确行为吗?

Wouldn't it be the correct behavior of Automapper to ignore the members and not throw an exception when verifying the validity of the configuration, as in the case of ignoring the property itself?

CreateMap<Type2, Type1>().ForMember(x => x.Prop3, y => y.Ignore());

推荐答案

AutoMapper从源映射到目标.据我所知,默认情况下,源中存在但目标类型中缺少的字段将被忽略.您需要显式处理的是源中缺少但目标类型中存在的字段.在您的情况下,TypeToIgnore在源中.因此,通过忽略该类型,目标中没有Prop3的源副本.

AutoMapper maps from source to destination. As far as I know by default fields present in source but missing in the destination type are ignored. What you have to explicitly handle are the fields missing in the source, but present in the destination type. In your case TypeToIgnore is in the source. Thus by ignoring that type, you have no source counterpart of Prop3 in the destination.

作为说明,这不会引发异常:

As an illustration, this will not throw exception:

ShouldMapProperty = p => p.PropertyType != typeof(TypeToIgnore);
CreateMap<Type1, Type2>();

这也不会:

public class TypeToIgnore { }

void Main()
{

    var config = new MapperConfiguration(cfg => cfg.AddProfile(new MappingProfile()));
    config.AssertConfigurationIsValid(); // won't throw
}

class Type1
{
    public int Prop1 { get; set; }
    public string Prop2 { get; set; }
    public bool Prop3 { get; set; } // <- because bool is ignored, you could simply delete this row
}

class Type2
{
    public int Prop1 { get; set; }
    public string Prop2 { get; set; }
    public TypeToIgnore Prop3 { get; set; }
    public long Prop4 { get; set; }
    public double Prop5 { get; set; }
}

class MappingProfile : Profile
{
    public MappingProfile()
    {
        ShouldMapProperty = p => p.PropertyType != typeof(bool);
        CreateMap<Type2, Type1>();
    }
}

这篇关于属性类型被忽略时Automapper的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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