This project is read-only.

Create a Specification

Basic Specification

To create a Specification, add the SpecExpress namespace and create a class that inherits from Validates<T> where T is the class that the specification is validating. The add one or more Validation Expressions written using the SpecExpress DSL. Validation Expressions are created in the Specification's constructor. For example:

public class ContactSpecification : Validates<Contact>
{
    public ContactSpecification()
    {
        Check(c => c.FirstName).Required();
        Check(c => c.LastName).Required();
    }
}

Multiple Specifications for a Type

A system can have multiple specifications for a single Type. By registering a specification with the ValidationCatalog, the developer doesn't need to specify which specification to use to validate an object because the Validation Catalog will determine it by using the Specification for the Type that matches the Type of object being validated. In the case of multiple specifications registered, for example ContactSpecification and DeleteContactSpecification, the Catalog requires that one of the specifications be identified as default.

If there are multiple Specification resolved by the Catalog for a Type within a Context when you attempt to validate an instance, it will throw a SpecExpressConfigurationException.

Default Specification

public class ContactSpecification : Validates<Contact>
{
    public ContactSpecification()
    {
        //Specify this Specification as the default specification for Contact
        IsDefaultForType();

        Check(c => c.FirstName).Required();
        Check(c => c.LastName).Required();       
    }
}

Additional Specification

public class DeleteContactSpecification : Validates<Contact>
{
    public ContactSpecification()
    {
        Check(c => c.Active).Required().And.IsFalse();
    }
}

Validate Instance

//This will use ContactSpecification because the Specification isn't specified and ContactSpecification is marked as Default.
var results = ValidationCatalog.Validate(contact);

//Validate using non-default Specification
var result = ValidationCatalog.Validate(contact, DeleteContactSpecification );

Reusing Specifications

There are multiple ways to design your specifications for reuse.

If you have existing type called Customer with a Specification called CustomerSpecification. If you add a new type called PremiumCustomer that inherits from customer, you can add a new specification that handles validating just the new properties, and designate which Specification you want to extend from. For example:


public class PremiumCustomerSpecification : Validates<PremiumCustomer>
{
        public PremiumCustomerSpecification()
        {
            //Include the validation rules for Customer Specification, in addition to new rules ExtendedCustomer
            Using<Customer, CustomerSpecification>();          
            Check( c => c.PremiumStatus).Required().And.IsTrue();
        }
}


Validating Specifications

During the development cycle, the Developer should call ValidationCatalog.AssertConfigurationIsValid() to evaluate all Specifications that have been registered with the container, checking for correctly formed Validation Expressions.

Last edited May 6, 2010 at 4:01 PM by TechnoAg, version 3

Comments

No comments yet.