Validation Application Block in Enterprise Library 3.0 - ValidationFactory Class - Part II
by David Hayden ( Microsoft MVP C# ), Filed: Enterprise Library 3.0 Tutorials
Last time in Part I:
I discussed using the Validation Facade Class that comes with the Validation Application Block in Enterprise Library 3.0 to validate your business objects, classes, etc. Using the Validation Facade Class is the easiest way to validate object instances:
ValidationResults results
= Validation.Validate(subscriber);
As I mentioned, that is one way to do validation, but not the only way. If you look at the Validation.Validate Method in the source code, you will see that it just makes a call to the ValidationFactory Class on your behalf, essentially abstracting away the use of factories.
ValidationFactory Class
Using that Subscriber Class from Part I:
public class Subscriber
{
private string _name;
[NotNullValidator]
[StringLengthValidator(1,200)]
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _emailAddress;
[NotNullValidator]
[EmailAddressValidator]
public string EmailAddress
{
get { return _emailAddress; }
set { _emailAddress = value; }
}
public Subscriber() {}
}
we can call the ValidationFactory Class directly to validate an instance of Subscriber:
// Create subscriber with bad email address
Subscriber subscriber = new Subscriber();
subscriber.Name = "David Hayden";
subscriber.EmailAddress = "bademail.com";
// Create IValidator
IValidator<Subscriber> subscriberValidator
= ValidationFactory.Create<Subscriber>();
// Validate
ValidationResults results =
subscriberValidator.Validate(subscriber);
// Check if valid
if (!results.IsValid)
{
// If not valid, loop through the ValidationResults...
foreach (ValidationResult result in results)
Console.WriteLine(string.Format("Key: {0},
Message: {1}", result.Key.ToString(), result.Message));
Console.ReadLine();
}
So, the Validation.Validation Facade call of:
ValidationResults results
= Validation.Validate(subscriber);
is the same as the ValidationFactory.Create call of:
IValidator<Subscriber> subscriberValidator
= ValidationFactory.Create<Subscriber>();
ValidationResults results =
subscriberValidator.Validate(subscriber);
You will find this same pattern in all the application blocks, so if you are familar with Enterprise Library, this will look second nature.
As mentioned in Part I, if you are just using attributes to validate classes and nothing is in the configuration file, you can use an alternative ValidationFactory call that just looks at attributes to avoid the creation of an IConfigurationSource that essentially has nothing in it with regards to the validation of the classes. This would be the CreateValidatorFromAttributes method
IValidator<Subscriber> subscriberValidator =
ValidationFactory.CreateValidatorFromAttributes<Subscriber>();
ValidationResults results =
subscriberValidator.Validate(subscriber);
Conclusion
Again, this is another way to use the Validation Application Block. Simply add ValidatorAttributes to your classes and use the ValidationFactory Class to validate them. In future tutorials, I will look at other ways to use the Validation Application Block.
by David Hayden ( Microsoft MVP C# )
Filed: Enterprise Library 3.0 Tutorials