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
.
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 = validat开发者_JAVA技巧ionService
.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?
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).
精彩评论