为什么 AutoMapper 验证不起作用,如果一个对象属性存在?

huangapple go评论90阅读模式
英文:

Why does AutoMapper validation not work if an object Property exists?

问题

如果我调用AutoMapper的AssertConfigurationIsValid()函数,它应该对映射类的每个属性抛出一个异常,如果其中一个属性使用未映射的类型,则应该抛出异常。但是似乎如果至少有一个属性的类型是object,则不会发生这种情况。

我理解AutoMapper无法检查是否对object属性可以分配所有已映射的类型,但我认为它仍然会对使用未映射类型的其他属性抛出异常。

如果我有以下这些简单的类:

public class RootLevel
{
    public object ObjectProperty { get; set; }
    public SecondLevel SecondLevel { get; set; }
}

public class RootLevelDto
{
    public object ObjectProperty { get; set; }
    public SecondLevelDto SecondLevel { get; set; }
}

public class SecondLevel
{
}

public class SecondLevelDto
{
}

并且(错误地)只映射了RootLevel类,并尝试执行以下代码:

var config = new MapperConfiguration(config =>
{
    config.CreateMap<RootLevel, RootLevelDto>();
});
config.AssertConfigurationIsValid();
var mapper = new Mapper(config);

var source = new RootLevel
{
    SecondLevel = new SecondLevel()
};
var mapped = mapper.Map<RootLevelDto>(source);

我期望调用config.AssertConfigurationIsValid()会抛出异常,因为类型SecondLevel没有映射。但只有当我调用mapper.Map<RootLevelDto>(source)时,才会抛出异常,而且只有在我的源对象实际上在SecondLevel属性中具有非null值时才会抛出异常。

如果我注释掉ObjectProperty属性,那么调用AssertConfigurationIsValid()会抛出预期的异常。

这导致了一个问题,我在测试过程中发现了缺少的映射,因为在测试期间该值始终为null

除了去掉object属性之外,是否有避免此问题的方法?

英文:

If I call the AssertConfigurationIsValid() function of AutoMapper it should throw an exception for each property of a mapped class, that uses a type that is not mapped. It seems like this does not happen, if at least one of the properties is of type object.

I understand that AutoMapper cannot check, if everything that can be assigned to a object property is mapped, but I assumed it would still throw an exception for other properties that use unmapped types.

If I have these simple classes:

public class RootLevel
{
    public object ObjectProperty { get; set; }
    public SecondLevel SecondLevel { get; set; }
}

public class RootLevelDto
{
    public object ObjectProperty { get; set; }
    public SecondLevelDto SecondLevel { get; set; }
}

public class SecondLevel
{
}

public class SecondLevelDto
{
}

And I (incorrectly) only map the RootLevel class and try to execute this code:

var config = new MapperConfiguration(config =&gt;
{
    config.CreateMap&lt;RootLevel, RootLevelDto&gt;();
});
config.AssertConfigurationIsValid();
var mapper = new Mapper(config);

var source = new RootLevel
{
    SecondLevel = new SecondLevel()
};
var mapped = mapper.Map&lt;RootLevelDto&gt;(source);

I would expect the call to config.AssertConfigurationIsValid() to throw an exception because the type SecondLevel is not mapped. But I only get the exception as soon as I call mapper.Map&lt;RootLevelDto&gt;(source), and only if my source object actually has a non-null value in the SecondLevel property.

If I comment out the ObjectProperty properties, the call to AssertConfigurationIsValid() throws the expected exception.

This lead to an issue where I discovered missing mappings too late, because the value was always null during testing.

Is there a way to avoid this issue, other than getting rid of the object properties?

答案1

得分: 2

这是一个错误,现在已经解决。尝试MyGet构建。

一个“脆弱”的解决方法是在类定义的末尾声明object属性。

英文:

That's a bug, solved now. Try the MyGet build.

A fragile workaround is to declare the object properties at the end of the class definition.

huangapple
  • 本文由 发表于 2023年8月4日 20:41:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定