0

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?

Chad
  • 23,658
  • 51
  • 191
  • 321

1 Answers1

1

You should decorate the properties of the TheJuvenileClientContactItem type with the ObjectValidatorAttribute, because Validation Application Block will not validate object graphs by default (to prevent performance problems and stack overflow exceptions).

Steven
  • 166,672
  • 24
  • 332
  • 435
  • Please see my edit regarding the error that I got when trying this: "ObjectValidatorAttribute can not ba applied because the attribute is not valid on this declaration type." – Chad Mar 23 '11 at 17:33
  • @Velika: You should place the attribute on the property in the `JuvenileClientContactItem` type, not on the `AddressType` itself. – Steven Mar 23 '11 at 19:45
  • @Velika: Please post the code of the `JuvenileClientContactItem` class. – Steven Mar 23 '11 at 21:38
  • 1
    @Velika: You need to remove the `ObjectValidator` attribute from `AddressType` (and JuvenileClientContactItem class if you added on there) and place the `ObjectValidator` attribute on the property or field you wish to validate. See http://msdn.microsoft.com/en-us/library/ff650581.aspx#Y1140 – Randy Levy Mar 23 '11 at 22:49
  • @Vekika: Show us the complete code, to see what you're doing. – Steven Mar 24 '11 at 10:53