Click here to Skip to main content
15,867,568 members
Articles / Web Development / ASP.NET

16 steps to write flexible business validation in C# using validation blocks

Rate me:
Please Sign up or sign in to vote.
4.36/5 (77 votes)
15 Mar 2009CPOL9 min read 176.9K   217   26
16 steps to write flexible business validation in C# using validation blocks

Updated with links of Other Application Blocks 

Table of Contents

Introduction

Validation is one of the most important parts in any software project. Building flexible business validation is every one’s dream. Rather than writing frameworks from scratch to do these things, Microsoft validation blocks makes it a breeze. In this article we will discuss how validation application blocks help us to build flexible validations using validation application blocks. Its just a simple sixteen step process to put our business validation in action using validation blocks.

Other Application Blocks

Client side validation: - One of the short comings in VAB is that it does only server side validations. This article talks how we can leverage VAB for client side. Client side validation

Dynamic validation: - This article explains how to build dynamic validation on scenario basis. Dynamic validation

Policy Application blocks: - This article talks how to implement plug and play mechanism using Policy application blocks. Policy application block

Logging application block: - This article explains the 5 basic steps of how to use logging application blocks.
Logging application block

Data application: - This article talks about the four steps you need to implement data application blocks.
Data application block

Exception application block: - This application talks how we can use exception application blocks to log exception from project. Exception application block

Unity application block: - This application talks about Unity Application Block in DI and IOC.
Unity application block

UIP block: - This article talks about Reusable Navigation and workflow for both Windows and Web using Microsoft UIP blocks. UIP block

The problem

No business can work without validation. Software’s are made to make business automated so validation forms the core aspect for any software. Almost 80% projects in .NET implement validation as shown in figure below.

Image 1

Figure: - Validation implemented

The above figure shows how business validations are implemented for a simple customer class. We have two properties customer code and customer name. Both of these properties are compulsory. So we have created a class called as ‘ClsCustomer’, with two set/get properties for customer code and customer name. If we find that customer code is empty in the set of the property we raise an error. The same methodology we have used for customer name.


Now we can consume the class in the ASPX UI as shown in figure ‘Business object consumed’. We have created the object, set the properties and in case there is error we have caught the same and displayed it in a label using try catch exception blocks.

Image 2

Figure :- Business object consumed

This implementation looks good to some extent as we have put the business validation in the class and decoupled the UI from any business validation changes.

Let’s list down the common problems associated with the above methodology:-

Tangled validation: - If the validation rules changes we need to compile the whole class. As the entity and the validation layer are entangled in one class, changes in validation leads to compilation of the whole class. So out first goal should be decouple this validation logic from the entity class. When we say entity class we are referring to the ‘ClsCustomer’ class.

Image 3

Figure :- Tangled validation

Image 4

Figure :- Decoupling validations from the entity class

No need of compiling: - Validations are volatile entity. They change from time to time. If we can change validation constraints without compiling will lead to better maintenance of the system.

One go validation and results: - The second issue is validations are executed in every set of the property and errors are displayed one by one. It would be great if we can validate all the validation once and display all the errors once.

All these things can be achieved by using Microsoft validation application blocks.

The solution: - validation application blocks

Step 1:- Download the Enterprise library 4.0 from here.

Step 2:- Once you install it you should see the same in programs – Microsoft patterns and practices.

Before we move ahead to understand how validation application blocks works. We will first walkthrough fundamentally how validation application blocks decouples the validation from the entity classes. Validation application blocks stands in between the entity class the validation. Validations are stored in configuration files like web.config or app.config depending whether it’s a web application or a windows application. So the validation blocks reads from the configuration file the rules and applies it over the entity class. We can maintain these validations using the enterprise configuration screen provided by Microsoft blocks.

Image 5

Figure: - Decoupling validations from entity classes

Now that we have understood how validation blocks will work. We will run through a example practically to understand the concept.

Step 3:- Once you have installed the block, click on Enterprise library configuration UI to define validation on the configuration file

Image 6

Figure :- Loading Enterprise configuration screen

Step 4:- Click on open and browse to your web.config file and click the open button.

Image 7

Figure :- Open the web.config file

Step 5:- Once you have opened the web.config file you should see a tree like structure as shown in figure ‘Add validation block’. Right click on the main tree and add the validation application block to your web.config file.

Image 8

Figure :- Add validation application block

Step 6:- Once you have added the validation application block you should see a validation application block node. Right click on the node , select new and click the type menu.

Image 9

Figure: - Add the assembly type

Step 7:- Once you have clicked on the type menu you should see a dialog box as shown in figure ‘Load assembly’.

Image 10

Figure: - Load assembly

Step 8:- Click on load assembly and browse to the assembly DLL to which you want to apply validations.

Image 11

Figure: - Browse the assembly

Step 9 :- Select the class in the assembly as shown in figure select class.

Image 12

Figure :- Select class

Step 10:- Once you have loaded the assembly, you should see a new node ‘MyRules’ as shown in figure ‘Choose members’. Right click on ‘MyRules’ ? new and choose members.

Image 13

Figure: - Choose members

Step 11:- Once you have clicked the choose members menu you will be popped up with a property selector box as shown in figure ‘Select properties’. In the customer class we have two properties on which we want to apply validations one is the customer name and the other is the customer code. So we will select both the properties and click ok.

Image 14

Figure: - Choose properties

Step 12:- Once you have selected the properties you can see both the properties in the UI. To apply validation right click on let’s say customer code property. You will now see list of validations which you can apply on the property. This tutorial will not go in details about all the validations. If you are interested in the same you can read about it more on http://msdn.microsoft.com/en-us/library/cc309336.aspx  . For the current scenario we will select range validator.

Image 15

Figure: - Select range validator

Step 13:- Once you have selected range validators, you will see the below property box for range validators. In this particular scenario we need that both the property i.e. customer code and customer name should not be empty. So in the lower bound we will put 1 , which signifies that at least one character is needed. In the lower bound type we have selected as inclusive, which means the lower bound check is compulsory. In the message template put the error message which you want to display in case there are issues.

Image 16

Figure: - Range validator properties

Step 14 :- In the default rule select the rule which you have just made. If it’s none validations will not fire.

Image 17

Figure: - Select rule

If you are done just save everything and watch your web.config which is now modified with the validation tags. You will see both the validation i.e. customer name and customer code in XML format inside the <validation> tag. The best part of the config file is now you can change the validation in the config file itself.You guessed right… no recompiling , no installation and completely decoupled.

Image 18

Figure: - Web.config with validation tags

Step 15:- Ok, now that we are done with putting the validation in the web.config file. Its time to write the code which will read from the config file and apply to our entity class. So the first thing we need to reference the validation application block in our assembly references and include the name spaces in our clsCustomer class.

Image 19

Figure: - Reference validation application blocks

Image 20

Figure: - Import the validation namespace

Step 16:- Once you are done with adding the namespaces its time to fire the validation. Below is the code snippet which shows how the validation application block fires the validation using the web.config file. First step create the customer object and set the property. In the second step we tell to the validation static class to validate the object. The validation static class returns validation results. This validation results are nothing but error failures in case the validation fails. In the final step we have looped through the validation results to display the error.

Image 21

Figure: - The validation code

Ok, now that we are done its time to see the action. You can see in the UI we have just set empty properties and it showed me both the business validations errors from the collection.

Image 22

Figure :- Validation in action

The other problem with validation is that it only does server side you can read my tutorial how we can generate client side validators using validation engine at ApplicationBlock.aspx

For Further reading do watch  the below interview preparation videos and step by step video series.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect https://www.questpond.com
India India

Comments and Discussions

 
Questionunable to load the assembly r .dll file Pin
Member 109356999-Jul-14 7:42
Member 109356999-Jul-14 7:42 
QuestionSeparate Validation Class Pin
Nikhil Contractor16-Jan-13 11:03
professionalNikhil Contractor16-Jan-13 11:03 
GeneralMy vote of 4 Pin
Nigam Patel22-Dec-11 19:21
Nigam Patel22-Dec-11 19:21 
QuestionHow to write rules into different file like text file other than .config file. Pin
krunaldesai12-Oct-11 2:54
krunaldesai12-Oct-11 2:54 
QuestionUsing your code with a localizable application ? Pin
Sancha` Prog9-Mar-10 1:29
Sancha` Prog9-Mar-10 1:29 
GeneralNice work Pin
Kuldip Rindani13-Aug-09 0:12
Kuldip Rindani13-Aug-09 0:12 
GeneralCode?Never put code why Pin
devnet24716-Mar-09 19:24
devnet24716-Mar-09 19:24 
GeneralRe: Code?Never put code why Pin
Shivprasad koirala16-Mar-09 20:05
Shivprasad koirala16-Mar-09 20:05 
Generalgreat article Pin
Donsw25-Jan-09 18:35
Donsw25-Jan-09 18:35 
GeneralGood one Pin
.netruler18-Dec-08 6:08
.netruler18-Dec-08 6:08 
GeneralRe: Good one Pin
Shivprasad koirala18-Dec-08 18:35
Shivprasad koirala18-Dec-08 18:35 
GeneralString Hint Pin
djhayman18-Nov-08 15:10
djhayman18-Nov-08 15:10 
GeneralLearning ASP.Net- help me!!! Pin
xuandu18-Nov-08 3:10
xuandu18-Nov-08 3:10 
GeneralGood Article Pin
Samer Aburabie17-Nov-08 22:15
Samer Aburabie17-Nov-08 22:15 
Questionreally good idea? Pin
christoph brändle4-Nov-08 22:24
christoph brändle4-Nov-08 22:24 
AnswerRe: really good idea? Pin
Dmitri Nеstеruk4-Nov-08 22:38
Dmitri Nеstеruk4-Nov-08 22:38 
QuestionPart 2? Pin
Mark Henke14-Oct-08 7:42
Mark Henke14-Oct-08 7:42 
AnswerRe: Part 2? Pin
Shivprasad koirala16-Oct-08 15:09
Shivprasad koirala16-Oct-08 15:09 
GeneralRe: Part 2? Pin
Mark Henke17-Oct-08 3:30
Mark Henke17-Oct-08 3:30 
QuestionHow to store the validation rules in multiple configuration files? Pin
Wilson Carchi A.14-Oct-08 4:42
Wilson Carchi A.14-Oct-08 4:42 
AnswerRe: How to store the validation rules in multiple configuration files? Pin
Mark Henke14-Oct-08 7:45
Mark Henke14-Oct-08 7:45 
AnswerRe: How to store the validation rules in multiple configuration files? Pin
Shivprasad koirala16-Oct-08 15:13
Shivprasad koirala16-Oct-08 15:13 
GeneralThanks Pin
kodali ranganadh6-Oct-08 21:20
kodali ranganadh6-Oct-08 21:20 
GeneralUseful stuff. Pin
Rajesh Pillai30-Sep-08 23:05
Rajesh Pillai30-Sep-08 23:05 
GeneralAdvice Pin
Warrick Procter30-Sep-08 12:17
Warrick Procter30-Sep-08 12:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.