We've started to actively used Microsoft Enterprise Library Validation Enterprise Block ( VAB) and I was surprised , that a few commonly used operations are not supplied(or I haven't found them) out of the box.
See two extensions, that make use of VAB simpler
public static class ValidationResultsExtensions
{
public static string CombinedMessage( this ValidationResults results)
{
string errorMessage = ( from res in results select String.Format(" {0}:{1} ", res.Key, res.Message)).ToDelimitedString( ";");
return errorMessage;
}
//Throws ValidationException if not valid public static void ValidateConstraints<T>( this T target)
{
Validator validator = ValidationFactory.CreateValidator<T>();
var results = new ValidationResults();
validator.Validate(target, results);
if (results.IsValid == false)
{
var errorMessage = results.CombinedMessage();
throw new ValidationException(errorMessage);
}
}
}v