Friday, September 18, 2009

What is SpecExpress?

SpecExpress is a Fluent Interface to a validation focused Domain Specific Language for .Net.

Benefits

  • SpecExpress handles all the infrastructure code allowing the Developer to focus on writing concise validation rules in the Validation DSL.
  • The Fluent Interface, teamed up with a DSL, allows the developer to quickly write the rules with the help of Visual Studio Intellisense which guides the Developer into writing consistent code.

Examples

Let’s look at some requirements for validating a person’s Last Name. Some typical validation rules are:

  1. Make sure it’s required by looking for nulls or empty strings
  2. Make sure it’s not too big to fit into the database
  3. Doesn’t have any funny characters

If any or all of these are broken, you have to display nice user-friendly feedback. It would be some combination of:

  • Last Name is required.
  • Last Name length must be less than 50 characters
  • Last Name can only contain letters

Now let’s look how we would implement this requirement with and without SpecExpress.

With SpecExpress

Check(p => p.LastName).Required()
.And.IsAlpha()
.And.MaxLength(50);

Pretty easy, huh? And now…

Without SpecExpress

var errors = new List<string>();
//Required
if (string.IsNullOrEmpty(input.LastName))
{
errors.Add("Last Name is required.");
}
else
{
//MaxLength
if (input.LastName.Trim().Length > 50)
{
errors.Add("Last Name length must be less than 50 characters");
}

//Only characters A-Z
var onlyValidChars = new Regex(@"^[a-zA-Z\s]+$")
.Match(input.LastName.Trim()).Success;

if (!onlyValidChars)
{
errors.Add("Last Name can only contain letters");
}
}

return errors;

Not too difficult, but a lot of code, most of it having to deal with “Infrastructure” code storing the error messages But there’s also Regular Expressions (yuck!), and some logic for only validating the rules if nothing is required.

Validation Catalog

In addition to a Fluent DSL, another feature that streamlines the validation process is the Validation Catalog. If you’re a fan of Dependency Injection, then you’ll love the Validation Catalog. Register your Specifications with the Catalog on startup, and when it comes time to validate your objects, just call

ValidationCatalog.Validate(object);

Add let the Validation Catalog resolve which specification is right for you.

You’ve piqued my interest. What now?

Take a look at the Quickstart if you want to see a full example of how to create a validation specification using SpecExpress, and validate an object.

Read the full Developers Guide, which has lot’s of examples, even if it still is a work in progress.

Or, if you want to jump right into slinging some code, Download the latest release.

Feedback

Even though a lot of love, thought, work, and testing has gone into this, we’re still interested in your feedback. Drop me a line to TechnoAg @ Twitter, post it to the Discussion Groups, or just drop a note in the comments.

No comments:

Post a Comment