The validation library provides a suite of attributes that extend Microsoft's own System.ComponentModel.DataAnnotations library. In addition, there are handy extension methods and a stand-alone validator.
Validation attributes can really help to make your code easier to read and much more intention revealing. For example, you can replace this:
public string Name
{
get { return name; }
set
{
if (value == null || value == string.NullOrEmpty())
{
throw new InvalidEntityException("You must supply a name!");
}
if (value.Any(char.IsWhiteSpace))
{
throw new InvalidEntityException("Names cannot contain whitespace!");
}
name = value;
}
}
With this:
[Required]
[NoWhitespace]
public string Name { get; set; }
Prevents any non-alpha (A-Z or a-z) characters.
Prevents non-alphanumeric characters.
Prevents extended ASCII characters.
Prevents values greater than a maximum value.
Prevents values less than than a minimum value.
Prevents strings longer than a maximum value.
Prevents strings shorter than a minimum value.
Prevents leading whitespace.
Prevents lower case characters.
Prevents trailing whitespace.
Prevents upper case characters.
Prevents whitespace characters.
Prevents non-numeric characters.
Prevents non-printable characters.
Provides handy extension methods such as object.Validate() and object.IsValid()
A stand-alone validator that will handle single or multiple entities.