The ConfigurationValidatorBase class

This appears on the the Microsoft 70-536 exam syllabus – section 3.1.6, but it’s one of those areas that’s not covered well by the offical book, or by anything I’ve managed to find on the Internet. This may be because it’s not often used, but it can add a layer of protection to your applications. Classes that extend ConfigurationValidatorBase are used with the ConfigurationValidator attribute to provide declarative validation for configuration properties. I’m going to create my own validator for instances of the UrlElement class that can be found in this article. Before we move on, it’s important that you know there are also some built in validators that you may be able to use. These include IntegerValidator and StringValidator, which validate ints and strings respectively. Both allow a minimum and maximum, with the StringValidator allowing you to specify invalid characters and the IntegerValidator allowing you to exlude a range as well as include one. You’ll also be pleased to know that if you’re a fan of RegularExpressions, there’s also a RegexStringValidator in there aswell.

RegexStringValidator could be used in this example to do all of our validation for us. However, it should be noted that we are simply demonstrating the implementation of ConfigurationValidatorBase using a simple example of a ConfigurationElement, rather than providing the quickest solution.

Although ConfigurationValidatorBase has only one abstract method that we need to implement – void Validate(object value), you will also need to override the method bool CanValidate(Type type). This lets the caller know whether a compatible validator has been declared against the property. So with these things in mind, we can build our validator like so:

  1. public class UrlElementValidator : ConfigurationValidatorBase
  2. {
  3.     public override void Validate(object value)
  4.     {
  5.         UrlElement val = (UrlElement)value;
  6.  
  7.         if (val.Name.Length < 1)
  8.         {
  9.             throw new ConfigurationErrorsException(
  10.                 "The length of UrlElement.Name was less than 1.");
  11.         }
  12.  
  13.         if(!val.Url.StartsWith("http://"))
  14.         {
  15.             throw new ConfigurationErrorsException(
  16.                 "UrlElement.Url did not start with http://.");
  17.         }
  18.     }
  19.  
  20.     public override bool CanValidate(Type type)
  21.     {
  22.         return (typeof(UrlElement) == type);
  23.     }
  24. }

Above, you can see above the use of the ConfigurationErrorsException. This exception type should be thrown if the object we are validating is invalid. This is the exception thrown when other built in validators fail to validate a value.

With the validator built, we can now decorate our property with the ConfigurationValidator attribute. It takes one argument of type Type, representing the type of the validator to use:

  1. // Start of ConfigurationSection code…
  2.  
  3. [ConfigurationProperty("url")]
  4. [ConfigurationValidator(typeof(UrlElementValidator))]
  5. public UrlElement Url
  6. {
  7.     get
  8.     {
  9.         return (UrlElement)base["url"];
  10.     }
  11. }
  12.  
  13. // ConfigurationSection code continued…

There, now try some values in your configuration file. You’ll see that if you use an empty name or a url that doesn’t start with http://, a ConfigurationErrorsException will be thrown with a nice descriptive message when calling ConfigurationManager.GetSection. Much better than finding a problem whilst trying to access an invalid URL.

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • email
  • LinkedIn
  • Technorati

Leave a Reply

Your email address will not be published. Required fields are marked *