英文:
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 =>
{
config.CreateMap<RootLevel, RootLevelDto>();
});
config.AssertConfigurationIsValid();
var mapper = new Mapper(config);
var source = new RootLevel
{
SecondLevel = new SecondLevel()
};
var mapped = mapper.Map<RootLevelDto>(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<RootLevelDto>(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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论