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);

Check out the Developers Guide for more.

Last edited Feb 24, 2010 at 3:23 PM by TechnoAg, version 10


foyzulkarim Sep 19, 2012 at 8:44 AM 
If I write "Check(c => c.FirstName).Required().And.MaxLength(100).And.IsAlpha();" then I got the following error:
'SpecExpress.DSL.WithRuleBuilder<Library.Contact,string>' does not contain a definition for 'And' and no extension method 'And' accepting a first argument of type 'SpecExpress.DSL.WithRuleBuilder<Library.Contact,string>' could be found.

But the following code works fine.
Error free Code: Check(p => p.FirstName).Required().MaxLength(100).And.IsAlpha();

I just reordered the clauses. Please solve issue or change the provided code.