We are using the Validation Application Block (VAB) from the Patterns and Practices team at a project I am currently working on. I haven't had a look at this for a while and I was very impressed with the integration they have with WCF in EntLib 4.0.

If I want to make sure that FirstName is validated to be between 1 and 20 I simply have to add a StringLengthValidator to the FirstName property and a ValidatorBehavior to the contract interface. I also have to specify a FaultContract of type ValidationFault on the operations which are to be validated.

using Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF;using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

VAB has a lot of great features which I am not going to cover in detail in this post but one of the features is that you can specify Ruleset's. This enables you to group rules together and to ultimately run different rules for different scenarios.

It is quite self explanatory what I want to do; On Create I want to make sure FirstName is specified and between 1 and 20 characters and that the customer has entered a username. The Id is not necessary since I am creating the customer. On the other hand when I Update the customer I need the Id, but the UserName is not necessary as this should not be updated. Nice and easy...

Now comes the only limitation that I would love to see in a future release of EntLib: The ruleset you wish to run you can only specify at the service contract level by adding a Ruleset parameter to the [ValidationBehavior] attribute. I would like to be able to specify different rulesets on different service operations to allow for even greater granularity.

In the next post I will describe how easy it is to add this feature to the EntLib implementation of the [ValidatorBehavior].