Validating a single Property on an Object explicitly

Topics: Validation Application Block
Sep 19, 2008 at 5:38 PM
I use the following code to validate an object explicitly:

                Dim results As ValidationResults = Validation.Validate(Of MyClass)(object)

I can access the Validation results for a particular property by iterating through results and checking for the Result key.

However, I have a situation, where my Object has large number of properties, with each property specifying multiple Validation attributes.
I need to validate only a single Property of this object, and do not want to use the above approach for efficiency reasons.

Is there any way to request validation of only a single Property of an object explicitly??

Sep 22, 2008 at 6:01 AM
After some more analysis of the Validation Block code, I found the PropertyValidationFactory class, that can be used to get a Validator for a Property, but that class is internal to the Validation assembly. Although I can easily change the code to make it public and recompile, still there should be a reason why the designers chose to keep it internal.

Can somebody please tell me how to do what I am trying to accomplish???
Sep 22, 2008 at 1:14 PM
The simplest thing you can do to accomplish this with the released bits is to use rulesets, although it's not an ideal solution (it adds overhead, it doesn't allow you to use rulesets for their intended purpose, it doesn't help validating all the properties at the same time, etc).

Sep 23, 2008 at 5:41 AM
Can you elaborate a bit more on the approach you are suggesting (preferably with a quick sample code as I have not used Rulesets till now)??
Sep 23, 2008 at 2:12 PM
Here are some links describing rulesets You could specify validators like this in your class (pseudocode):

public class Foo
   [BarValidator(Ruleset = "Prop1")]
   public string Prop1 { get; set; }

   [BazValidator(Ruleset = "Prop2")]
   public string Prop2 { get; set; }

Sep 23, 2008 at 4:24 PM
I am sorry, I made that post in haste. Well, I am using RuleSets extensively.
What I am looking for is a method to validate a single property on an object. Also, are there any ideas why PropertyValidationFactory has been kept as internal???