Validation Application Block - Rules in External XML Configuration File - App.Config Web.Config - Enterprise Library 3.0 - Part IV

Validation Application Block - Rules in External XML Configuration File - App.Config Web.Config - Enterprise Library 3.0 - Part IV

by David Hayden ( Florida .NET C# SQL Server Developer ), Filed: Enterprise Library 3.0 Tutorials

 

Currently as I am writing this tutorial there is no support for the Validation Application Block in the Configuration Tool that ships with Enterprise Library 3.0 December 2006 CTP. There are also no quickstarts for the Validation Application Block :) So, if you want to configure your validation rules in an XML file in the current CTP, you will need to manually add the information to your app.config, web.config, or separate XML file. Later on you will be able to do this with the configuration tool that ships with Enterprise Library 3.0.

 

No Need for ValidatorAttributes

If you have been following the previous tutorials:

we have been decorating our classes with ValidatorAttributes that ship with the Validation Application Block. The ValidatorAttributes allow you to specify validation rules on your classes without the need of an external rules file:

 

public class Subscriber
{
    private string _name;

    [NotNullValidator]
    [StringLengthValidator(1,200)]
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    private string _emailAddress;

    [NotNullValidator]
    [StringLengthValidator(1,200)]
    public string EmailAddress
    {
        get { return _emailAddress; }
        set { _emailAddress = value; }
    }

    public Subscriber() {}
}

 

You can see the ValidatorAttributes, NotNullValidator and StringLengthValidator, decorating the Name and EmailAddress properties. Now that we are going to be switching from ValidatorAttributes to specify our validation rules in the app.config file, say goodbye :) Here is the new Subscriber Class:

 

public class Subscriber
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    private string _emailAddress;
    public string EmailAddress
    {
        get { return _emailAddress; }
        set { _emailAddress = value; }
    }

    public Subscriber() {}
}

 

Validation Rules in App.config

As with just about every application block in Enterprise Library, you can specify just about everything in a configuration file and the Validation Application Block is no different. Here are the same rules mentioned above using a configuration file:

 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="validation" type="Microsoft.Practices.
EnterpriseLibrary.Validation.Configuration.
ValidationSettings,
Microsoft.Practices.EnterpriseLibrary.Validation
"/> </configSections> <validation> <type name="EntLib3.Subscriber" defaultRule="default"> <rule name="default"> <properties> <property name="Name"> <add name="NameNotNull" type="Microsoft.Practices.
EnterpriseLibrary.
Validation.Validators.NotNullValidator,
Microsoft.Practices.
EnterpriseLibrary.Validation
" /> <add name="NameLength" type="Microsoft.Practices.
EnterpriseLibrary.
Validation.Validators.StringLengthValidator,
Microsoft.
Practices.EnterpriseLibrary.Validation
"
lowerBound="1"
upperBound="200" lowerBoundType="Inclusive"
upperBoundType="Inclusive"/> </property> <property name="EmailAddress"> <add name="EmailAddressNotNull" type="Microsoft.
Practices.
EnterpriseLibrary.Validation.Validators.
NotNullValidator,
Microsoft.Practices.EnterpriseLibrary.
Validation
" /> <add name="EmailAddressLength" type="Microsoft.
Practices.
EnterpriseLibrary.Validation.Validators.
StringLengthValidator,
Microsoft.Practices.EnterpriseLibrary.
Validation
"
lowerBound="1"
upperBound="200" lowerBoundType="Inclusive"
upperBoundType="Inclusive"/> </property> </properties> </rule> </type> </validation> </configuration>

 

The config files is pretty self-explanatory as we have already been through most of this with the ValidatorAttributes in the earlier tutorials. A couple of things of interest:

  • You have to specify the class type to validate: EntLib3.Subscriber
  • You can specify the default rule ( Ruleset in ValidatorAttributes ) to use for validation: default

 

Validation Facade or ValidationFactory Class

As mentioned in the other tutorials, you can validate an instance of Subscriber using the Validation Facade Class or the ValidationFactory Class as such:

 

Validation.Validate(subscriber);

 

or

 

IValidator<Subscriber> subscriberValidator
= ValidationFactory.CreateValidator<Subscriber>(); ValidationResults results =
subscriberValidator.Validate(subscriber);

 

Now because we are only using the configuration file to specify rules and not any attributes on the class, we can keep the Validation Application Block from doing any reflection by specifying methods only related to IConfigurationSource:

 

ValidationResults results =
Validation.ValidateFromConfiguration(subscriber);

 

or

 

IValidator<Subscriber> subscriberValidator =
ValidationFactory.
CreateValidatorFromConfiguration
<Subscriber>(); ValidationResults results =
subscriberValidator.Validate(subscriber);

 

Multiple Rules

Just like with using ValidatorAttributes, you can specify multiple rules with the configuration file. I think you can imagine how this would work by specifying multiple rules:

 

<validation>
    <type name="EntLib3.Subscriber"
defaultRule="default"> <rule name="default"> ... </rule> <rule name="Persistence"> ... </rule> </type> </validation>

 

Conclusion

If you haven't already, I recommend checking out the 3 other tutorials above. If you read all four of the tutorials written thus far, you should feel pretty comfortable using the new Validation Application Block in Enterprise Library 3.0. I am using the first drop, December 2006 CTP, so I expect some changes as well as a few new enhancements. As this happens, I will mention any changes when appropriate. I am thinking about writing some real-world tutorials on how to implement this block in your applications, too.

by David Hayden ( Florida .NET C# SQL Server Developer )

Filed: Enterprise Library 3.0 Tutorials

posted on Monday, December 25, 2006 9:42 PM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices