Check.That validation framework reworked

Published on Jan 23, 2009

I just finished a complete re write of my validation framework. This re write was due to feedback received from some of my co workers so I owe to them my thanks. The validator is not an static class anymore, so you need to create an instance of it.

This allow to do multiple checks at once. Like this.

In this case I call the execution method List() to return a list that contains all the exceptions found. Actually it returns an IDictionary<string,List<Exception>> where the key is the name of the variable we are checking. In this case I’m using the CheckCondition that returns a custom Exception per error, this are Generic methods that need an object[] for the constructor parameters to use in the custom exceptions and a Type that inherits from Exception.

The framework also has a simple signature, where the CheckCondition will raise a pre defined Type of Exception, building the Exception message with enough detail in it, like variable name, actual value and expected value or value range.

This will return the following exceptions with these messages:

The new validator also allows us to check multiple variables at the same time, like this.

Throw will throw an exception of type ErrorsCollectionException, this class inherits from Exception but contains a property called ErrorsCollection that exposes the same IDictionary<string,List<Exception>> you can access using List().

This is useful in cases where you want to communicate all errors at once, for example when an end user is entering data in a form and you want to send all the errors back so he doesn’t have to fix each one at a time.

Sometimes you want to check only one thing and throw the specific exception for that given check and not the ErrorsCollectionException, this is very useful to program by contract, where each exception should be handled independently.

What if you want to do multiple checks and throw only one custom exception? Well you do it like this:

This also applies when checking more than one variable.

The code can be download as always from: http://code.google.com/p/latrompa/