This project is read-only.

Validation Contexts

Often you may want to group a set of Specifications together to represent a global context. A common example may be that you have different validation rules for saving and deleting. By defining a set of Specifications in a Validation Context, you can validate an object with the Validation Catalog and resolving Specifications against the Validation Context.

Define a Validation Context

public class DeleteValidationContext : SpecExpress.ValidationContext
       public DeleteValidationContext()
           //Add all specifications from ValidationCatalog that begin with 'Delete' to the Context
           AddSpecifications(cfg => cfg.Where(s => s.GetType().Name.StartsWith("Delete") == true));
            //Add a single Specification 

Validate using Context

var results = ValidationCatalog<DeleteValidationContext>.Validate(customer);


NHibernate has Pre Insert, Update, and Delete Event Listeners. You can create a new Delete Event Listener and validate the object given the context, without ever know what the object type is. This approach has the advantage of never depending on the developer to explicitly call validate before persisting through your repository.

Last edited Feb 23, 2010 at 6:23 PM by TechnoAg, version 2


moh_abed Sep 24, 2011 at 1:48 AM 
Hey there,
Any idea how I can manage this in client side too?

Let's say I have two forms in an MVC app page, and I want each to be validated individually on the client side. Can I use Validation contexts for this (How)? Is there a better alternative?

Thanks a lot.