Validation app block Best usage

Topics: Validation Application Block
May 12, 2009 at 7:01 PM

Impressed with Validation application block, but it does requires to write Validations for each Attribute (Domain) in Domain class. I mean to say, I have generated a C# class(Domain Objects class) using SQL to SQL Metal tool. This class contains all the columns from the Database Tables as properties, so it allows us to access those properties and write LINQ to SQL statements quite easily. so far so good.

Now I am using Validation application block in a Class that Inherits base class generated using SQL Metal tool above.  Thats how I am getting all Domain Objects (Database Tables columns) into Inherited class and writing Validation rules for each property.

But what I am not happy is to write these validation rules for all the Domain Objects where ever I need validations as shown below:


[StringLengthValidator(1, 50, Ruleset="RuleSetA", MessageTemplate="Last Name must be 1-50 characters")]
public string LastName
get { return lastName; }
set { lastName = value; }

[RegexValidator(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", MessageTemplate="Invalid e-mail address",
public string Email
get { return email; }
set { email = value; }

[ObjectValidator("RuleSetA", Ruleset="RuleSetA")]
public Address Address
get { return address; }
set { address = value; }
Say I have 100 Tabels in Database and each Table has got 10 columns. In order to consider Validation Application block, should I write
100 X 10 = 100 Rules on each Domain Object?
I strongly beleive that there is a best approach for this.
Please let me know.
May 13, 2009 at 1:29 PM


As far as I know, the answer will be yes you would have to. If you prefer not write, you can do it thru the config.

Valiant Dudan
Global Technology & Solutions
Avanade, Inc.

May 13, 2009 at 1:32 PM

Let me know how it can be done through config .

Your input is very much appreciated.


May 14, 2009 at 6:01 AM

It's actually better to defined validation rules in the config so that if you decided to change them later, you won't have to recompile your application.

The steps on how to defined validation rules in config is included in the documentation.  You can simply add the Validation Applicaton Block.  Right click, on its node, select New -> Type.  Browse for the assembly where your class you want to validate is defined in.  Select then Click Ok.  Right click then on the type's node, select Choose Members and then you'll be able to select the properties of that class and associate validators for them.  Check out the documentation for more information.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.