Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

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!

Comments

Thomas Eyde said:

Can we add rules code-only?

# November 15, 2009 7:43 PM

DigiMortal said:

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?

# November 16, 2009 4:58 AM

Thomas Eyde said:

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.

# November 16, 2009 8:46 AM

DigiMortal said:

You can find some information here: www.devx.com/.../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.

# November 16, 2009 9:58 AM