执行业务对象的验证。那么里面的物体呢?
我是企业库的新手。我正在尝试验证 JuvenileClientContactItem
类型的业务对象。
JuvenileClientContactItem
对象本身包含对象,在本例中是 AddressType
类型的对象的两个实例。
当我执行以下调用来验证 JuvenileClientContactItem
时,我预计所有包含的对象也会得到验证,并且 PersonType
对象中遇到的任何错误都将添加到 >ValidationResults
集合,但仅执行对 JuvenileClientContactItem
对象的验证。
validationResults = validationService
.Validate(Of JuvenileClientContactItem) _
(juvenileClientContactItem, _
"JuvenileClientContactItemRuleSet", "PersonTypeRuleSet")
以下是企业库验证函数的签名:
Public Shared Function Validate(Of T)(ByVal target As T, _
ByVal ParamArray rulesets() As String) _
As Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResults
要验证 JuvenileClientContactItem
对象中的两个 Address
子对象,我需要对 Validate
执行单独的调用code> 方法,例如:
residentaddressValidationResults = validationService
.Validate(Of BusinessObjects.AddressType) _
(juvenileClientContactItem.ResidenceAddress, _
Me.View, "AddressTypeRuleSet")
我很想编写自己的通用验证方法,该方法使用反射来检查传递给验证的对象,查找支持自我验证的包含对象,并执行调用并将所有验证的结果聚合到单个返回的集合中。有更好的方法吗?
编辑:
按照下面提到的建议,我添加了以下代码:
<System.Serializable()> _
<DataContract()> _
<HasSelfValidation()> _
<ObjectValidator()> _
Public Class AddressType
...并得到了无法应用 ObjectValidatorAttribute 的错误,因为该属性在此声明类型上无效。
为什么?我该如何纠正它?
I am new to Enterprise Library. I am trying to validate a Business Object of type JuvenileClientContactItem
.
TheJuvenileClientContactItem
objects itself has contained objects, in this case, two instances of objects of AddressType
type.
When I perform the following call to validate the JuvenileClientContactItem
, I expected that all contained objects would also be validated and any errors encountered in the PersonType
object would be added to the ValidationResults
collection, but only the validations on the JuvenileClientContactItem
oject were performed.
validationResults = validationService
.Validate(Of JuvenileClientContactItem) _
(juvenileClientContactItem, _
"JuvenileClientContactItemRuleSet", "PersonTypeRuleSet")
Here's the signature of the Enterprise Library Validate function:
Public Shared Function Validate(Of T)(ByVal target As T, _
ByVal ParamArray rulesets() As String) _
As Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResults
To validate the two Address
sub objects within the JuvenileClientContactItem
object, I need to perform separate calls to the Validate
method, eg:
residentaddressValidationResults = validationService
.Validate(Of BusinessObjects.AddressType) _
(juvenileClientContactItem.ResidenceAddress, _
Me.View, "AddressTypeRuleSet")
I am tempted to write my own generic validation method that uses reflection to examine the object passed for validation looking for contained objects that are support self validation and perform calls and aggregate the results of all validations into a single returned collection. Is there a better approach?
Edit:
Following the suggestion mentioned below, I added this code:
<System.Serializable()> _
<DataContract()> _
<HasSelfValidation()> _
<ObjectValidator()> _
Public Class AddressType
...And got the error that the ObjectValidatorAttribute can not ba applied because the attribute is not valid on this declaration type.
Why? How do I correct it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用
ObjectValidatorAttribute
修饰TheJuvenileClientContactItem
类型的属性,因为验证应用程序块默认情况下不会验证对象图(以防止性能问题和堆栈溢出异常)。You should decorate the properties of the
TheJuvenileClientContactItem
type with theObjectValidatorAttribute
, because Validation Application Block will not validate object graphs by default (to prevent performance problems and stack overflow exceptions).