Quickstart
Given this basic Domain Entity:
public class Contact
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string SpouseName { get; set; }
public bool Married { get; set; }
}
1. Create Validation Specification
public class ContactSpecification : SpecExpress.Validates<Contact>
{
public ContactSpecification()
{
Check(c => c.FirstName).Required().And.MaxLength(100).And.IsAlpha();
Check(c => c.LastName).Required().And.MaxLength(100).And.IsAlpha();
Check(c => c.MiddleName).Optional().And.MaxLength(100).And.IsAlpha();
Check(c => c.SpouseName).Required().If( c => c.Married).And.IsAlpha();
}
}
2. Initialize Validation Catalog
Add the ContactSpecification to the catalog of validation specifications. The code below will scan all assemblies in the AppDomain for all specifications and add them to the catalog.
This code should be placed in the application startup code, such as Global.asax.
//Find specifications in all Assemblies in AppDomain
ValidationCatalog.Scan(x => x.AddAssemblies(AppDomain.CurrentDomain.GetAssemblies().ToList()));
3. Validate
Given an instance:
var contact = new Contact {FirstName = "Joe", LastName = string.empty};
//Validate instance
var validationResults = ValidationCatalog.Validate(contact);