Print

Putting the Validation Application Block configuration in its own file

This article describes how to extract the validation configuration to its own file and allow this to also work with unit tests.

The problem that you encounter pretty quickly when defining the VAB configuration in the normal web.config or app.config is that you need to have multiple copies of that configuration. You'll have at least two copies: one for your unit tests and one file for the application itself. At first this is a bit annoying, but it get's frustrating quickly after that. Later on it leads to errors, after someone on your team updated the unit test configuration, but forgot to update the application’s configuration file.

Luckily the Enterprise Library allows you to choose alternative configuration files using the IConfigurationSource interface. Later more on this, but first here is how you put the VAB configuration in it’s own file:

Start by adding a new configuration file to on of your projects (most likely on of your business layer projects). After you created it, change the "Copy to Output Directory" property to "Copy if newer" by right-clicking on the file and choosing ‘properties’. This will enable the configuration file to be copied to the output directory, which isn't the default option.

Copy to Output Directory

After you've created the file you can simply add validation configuration the way you're used to using the Enterprise Library Configuration tool by right clicking on the configuration file and selecting "Edit Enterprise Library Configuration", or by editing the file by hand.

Edit Enterprise Library Configuration

Choosing "Copy if newer" for the validation configuration file has no effect in conjunction with Visual Studio Unit Tests. So the last step to take is to configure the test system to copy that file. The standard way to do this is by specifying the deployment items in the .testrunconfig file. You can do this by going to Test / Edit Test Run Configurations / Deployment. You can use the "Add File..." button to add the configuration file. The image below shows the deployment tab with a validation.config added.

Edit Test Run Configuration

<UPDATE 2010-01-06>
I forgot to mention how to exactly let the VAB know you replaced the configuration file.

You need to create an instance of a type implementing IConfigurationSource and supply it to an overload of the ValidationFactory.CreateValidator method. Using a configuration file named "validation.config", the following code snippet can be used to create a Validator based on that configuration:

string ruleSet = string.Empty;

IConfigurationSource configurationSource =
new FileConfigurationSource("validation.config");

var validator = ValidationFactory.CreateValidator(type, ruleSet,
configurationSource);

Or when using the IEntityValidator approach used in part 4 of my VAB series, the VABEntityValidator would look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation;

sealed class VABEntityValidator : IEntityValidator
{
private static IConfigurationSource ConfigurationSource =
new FileConfigurationSource("validation.config");

public IEnumerable<ValidationResult> Validate(
IEnumerable<object> entities)
{
return
from entity in entities
let type = entity.GetType()
let validator = CreateValidator(type)
let results = validator.Validate(entity)
where !results.IsValid
from result in results
select result;
}

private static Validator CreateValidator(Type type)
{
string ruleSet = string.Empty;

return ValidationFactory.CreateValidator(type, ruleSet,
ConfigurationSource);
}
}

</UPDATE>

Happy validating!

- Enterprise Library, Validation Application Block - three comments / No trackbacks - §

The code samples on my weblog are colorized using javascript, but you disabled javascript (for my website) on your browser. If you're interested in viewing the posted code snippets in color, please enable javascript.

three comments:

Hi Steven. I was wondering how one references this config file in another project other than in Unit Testing? I want to use my validators at a couple of differnt places in a couple of different projects in the same solution, aside from Unit Testing. If I define all of my VAB rule sets in one file, how can I get to that file from these different development projects in Visual Studio 2008?

Thanks,

Chris
Chris - 06 01 10 - 13:55

Hi Chris, the article was missing some information about how one should actually use the IConfigurationSource. I updated the article. I hope this answers your question. Please let me know if that's not the case.
Steven (URL) - 06 01 10 - 21:22

That was the piece I was missing, thanks!
Chris - 07 01 10 - 15:41


No trackbacks:

Trackback link:

Please enable javascript to generate a trackback url


  
Remember personal info?

/

Before sending a comment, you have to answer correctly a simple question everyone knows the answer to. This completely baffles automated spam bots.
 

  (Register your username / Log in)

Notify:
Hide email:

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.