简体   繁体   中英

Performing validation of a Business object. What about the contained objects?

I am new to Enterprise Library. I am trying to validate a Business Object of type JuvenileClientContactItem .

The JuvenileClientContactItem 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?

您应该使用TheJuvenileClientContactItem装饰TheJuvenileClientContactItem类型的ObjectValidatorAttribute ,因为默认情况下,验证应用程序块不会验证对象图(以防止性能问题和堆栈溢出异常)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM