说有class A
一个参考class B
像这样
class A
{
[NotNull]
string Alpha;
B bObject;
}
class B
{
[NotNull]
string Beta;
}
A a = new A();
a.Alpha = "test"
a.bObject = new b();
a.bObject.Beta = null;
现在,当为 A 的实例调用 Create 之类的东西时,代码调用NHibernate.Validator.Engine.ValidatorEngine.Validate
但是,它只验证A (Alpha)
. 当NHibernate
尝试持久化对象时,它会失败,因为数据库不允许null为Object
.
是否可以对其进行配置,以便验证A
也验证 的属性B
?
*编辑:
我目前解决这个问题的方法是手动调用Validate(A.bObject)
存储库需要创建/更新的任何地方A
,例如:
在存储库中A
public void Update(A entity)
{
//I want to avoid having to do this in every class repository that has a
//reference to B
var errorsList = valEngine.Validate(entity.bObject);
if (errorsList .Length == 0)
{
status = base.Create(entity);
//Simply calls the appropriate NH function to persist the object
}
}
如何避免重复B
每次验证的行?