Loading Enterprise Library validation rules from external file

Keeping validation rules in web.config may grow it pretty long and it is not convenient to make changes there anymore. Developers can manage validation rules easily by using Enterprise Library Configuration tool and therefore there is no good reason to handle validation rules manually. In this posting I will show you how to load validation rules from external configuration files.

Creating file for validation rules

Create new XML-file to your project and name it as Validation.config. I suggest .config extension because IIS blocks all requests to configuration files. Run Enterprise Library Condiguration tool and insert some validation rules.

Enterprise Library: Configuration tool

Save changes and close configuration tool.

Loading validation rules from external file

Now open the class where you handle validation logic. In my last posting ASP.NET MVC: Validating objects using Enterprise Library validation application block I gave you some idea about how to use validation in your application. There was class called Validator that took object and validated it. All validation errors are returned as array.


public static class Validator

{

    public static ValidationError[] Validate<T>(T instance)

    {

        var errors = new List<ValidationError>();

        var results = Validation.Validate<T>(instance);

 

        foreach (var result in results)

        {

            var error = new ValidationError();

            error.PropertyName = result.Key;

            error.Message = result.Message;

            errors.Add(error);

        }

 

        return errors.ToArray();

    }

}


In Validate<T>() method all validation rules are read from web.config. We have to modify this method to make it read validation rules from Validation.config. Here is the class with modifications.


public static class Validator

{

    public static ValidationError[] Validate<T>(T instance)

    {

        var errors = new List<ValidationError>();
 

        var source = new FileConfigurationSource("Validation.config");

        var validator = ValidationFactory.CreateValidatorFromConfiguration<T>(source);

        var results = validator.Validate(instance);

 

        foreach (var result in results)

        {

            var error = new ValidationError();

            error.PropertyName = result.Key;

            error.Message = result.Message;

            errors.Add(error);

        }

 

        return errors.ToArray();

    }

}


Validation rules are now read from Validation.config file and your web.config is lighter than before. Enterprise Library made this move very easy – only couple of lines of code were modified. Keeping validation rules in separate file is also safer – you don’t have to touch web.config to make modifications to validation rules.


kick it on DotNetKicks.com pimp it 顶 Progg it Shout it
Shout it!

4 Comments

  • Can we add rules code-only?

  • Hi Thomas!

    I'm not sure how it is possible but I'm almost sure it is possible. You are interested in code level configuration as I understand. Am I correct?

  • Yes, I prefer to do my configuration in code, except for things that truly change between deployment environments, like database connection strings and web service end points.

    Business rules, otoh, should not change between development, test or production. And I believe rules in code are easier to unit test than rules in xml.

  • You can find some information here: http://www.devx.com/dotnet/Article/34950/1954 You can use validation attributes on your classes but personally I don't like this solution because your classes depend on validation framework then.

Comments have been disabled for this content.