Home > View Post

Validation Application Block and WCF

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.

Consider the following service and data contract:


[ServiceContract]
public interface ICustomerRepositoryService
{
    [OperationContract]
    void Update(Customer customer);

    [OperationContract]
    string Create(Customer customer);
}

[DataContract]
public class Customer
{
    [DataMember]
    public string Id { get; set; }

    [DataMember]
    public string FirstName { get; set; }

    [DataMember]
    public string UserName { get; set; }
}

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;

[ServiceContract]
[ValidationBehavior]
public interface ICustomerRepositoryService
{
    [OperationContract]
    [FaultContract(typeof (ValidationFault))]
    void Update(Customer customer);

    [OperationContract]
    [FaultContract(typeof (ValidationFault))]
    string Create(Customer customer);
}
// class code omitted for brevity

[DataMember]
[StringLengthValidator(1, RangeBoundaryType.Inclusive, 20, RangeBoundaryType.Inclusive, MessageTemplate = "Firstname must be between 1 and 20.")]
public string FirstName { get; set; }

This will work out of the box, no configuration required. Sweet!

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.

So in the end my contract definition looks like this:


[ServiceContract]
[ValidationBehavior]
public interface IValidationService
{
    [OperationContract]
    [FaultContract(typeof (ValidationFault))]
    void Update(Customer customer);

    [OperationContract]
    [FaultContract(typeof (ValidationFault))]
    CustomerKey Create(Customer customer);
}

[DataContract]
public class Customer
{
    [DataMember]
    [NotNullValidator(Ruleset = "Update")]
    public string Id { get; set; }

    [DataMember]
    [StringLengthValidator(1, RangeBoundaryType.Inclusive, 20, RangeBoundaryType.Inclusive,
        MessageTemplate = "Firstname must be between 1 and 20.", Ruleset = "Update")]
    [StringLengthValidator(1, RangeBoundaryType.Inclusive, 20, RangeBoundaryType.Inclusive,
        MessageTemplate = "Firstname must be between 1 and 20.", Ruleset = "Create")]
    public string FirstName { get; set; }

    [DataMember]
    [StringLengthValidator(1, RangeBoundaryType.Inclusive, 20, RangeBoundaryType.Inclusive,
        MessageTemplate = "Username must be between 1 and 20.", Ruleset = "Create")]
    public string UserName { get; set; }
}


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].

Tags: WCF

 
Bruusi Post By Bruusi
12:55 AM
15 Aug 2008

» Next Post: Rulesets on Operations in WCF with Validation Application Block
« Previous Post: Avoiding Primitive Obsession to tip developers into the pit of success

Comments are closed for this post.

© 2005 - 2017 Josh Twist - All Rights Reserved.