Automatic Validation of Objects


Introduction

 

Client side validations are inevitable for any project development.  Especially when we deal with objects, we are forced to do validations of values set to its properties (or public members) before doing any data layer activities.

 

Problem

 

A couple of projects I recently worked on had many validations to do for the business objects, that were very much too frequent. We were forced to do the validation logic whenever there were a value change occuring. Sometimes we forgot which resulted in a SqlException. 

 

That caused me to think, how better it would be if there is an automatic validation mechanism in place for my objects!!!  It should be like just setting the conditions in my class and the objects have to be taken care off.  Attributes gave me a hand.

 

Concept

 

ValidationAttribute is the class which is used to define our business classes for validations. Each public member and property can be set for its own validation rule.

 

ValidationAttribute class has two properties; Condition and ErrorMessage.  Condition is an enumerated type that describes the type of validation you require and ErrorMessage holds the message that needs to be shown to the user if in case the validation has failed.

 

Our business classes will be decorated with a ValidationAttribute tag along with the two properties; message and condition.  On a different note, I would like to add; this is meant for only web applications with 2.0 framework. As a disclaimer, this code is just halfway through and I am yet to include as many rules as I could think of.

 

Class diagram

 

 

 

 

 

Example - A quick start

 

Take an example of a Customer class that has two properties Name and EmployeeID.  The conditions are:

1.      Customer name is mandatory. Hence check for Null and Empty

2.      EmployeeID should accept only integer values.

 

Step 1: Our first step is to make the Customer class inherited from Attributable which is an abstract class.  Our business class is all set for its automatic validation.

 

Step 2:  Set Condition and required Error message.

Add attributes for the properties (or public members) as shown below.

 

public class Customer:Attributable

{

   

    private string _Name;

    [ValidationAttribute("Name cannot be empty", ValidationAttribute.Conditions.IsNotEmpty)]

    [ValidationAttribute("Name cannot be null", ValidationAttribute.Conditions.IsNotNull)]

    public string Name

    {

        get { return _Name; }

        set { _Name = value; }

    }

         

 

    [ValidationAttribute("Employee Id has to be integer", ValidationAttribute.Conditions.IsNumeric)]

    public string EmployeeId;

}


 

Notice that you are allowed to add as many numbers of rules as needed.

 

That's it. You are all set to use your object in your presentation layer.

 

Step 3:  And from my ASPX page?

 

  Customer c = new Customer();

        c.EmployeeId = "ABC";       // this should throw me an exception

        c.Name = "dfgdsg";          // this is valid. Try changing this to empty

   c.IsValid();

  

As your Customer class is inherited from Attributable, you would find a method IsValid() available in your Customer object.

 

c.IsValid(); will tell you whether your object is valid or not, according to the conditions you set.

 

You are free to extend your own customized rules as you wish. You can very well extend the enumerated type and add your logic in the ProcessFields method of the Attributable class.

 

You can iterate through each message from the Exceptions property.

 

  if (c.Exceptions != null)

        {

            foreach (AttributeException arEx in c.Exceptions)

            {

                Response.Write(arEx.Message + "<BR>");

            }

        }


 

Notice that c.Exceptions will always be null as long as the object satisfies all the rules you've decorated with attributes.

 

Hope you find this interesting and useful. Do write to me in case of any clarifications...


Similar Articles