In previous article, we had discussed about attribute based validators and rule sets. Now, we look into validation through config files. We will discuss about Enterprise Library Configuration tool followed by an example. Create a new Console Application in VS 2008 and name it as ConfigBasedVABClient.
Add new properties to Employee class and add it to a new class Library ConfigBasedVABServer as shown below:
Build the application. Go to Enterprise Library Configuration tool present in installation folder or Start Programs Microsoft patterns & practices Enterprise Library 4.1 - October 2008.
Click on New Application and select Validation Application Block as shown below:
- Select New Type Load From File Select ConfigBasedVABServer dll path Select Employee Ok.
- Select New Rule Set Name as Group1
- Right Click Group1 New Choose Field Check EmpID.
- Right Click EmpID New Range Validator Set required properties as shown below:
- Now save it as ConfigurationVAB.
Similarly, we can add other validators on Employee properties.
Go to ConfigBasedVABClient and add reference to ConfigBasedVABServer and required VAB dlls discussed in previous article.
Add app.config to the client project and paste the contents of ConfigurationVAB.config into it as shown below:
Add the below code to Main method:
Run the application, the output will be like this:
In this way, we can add our validators to existing assemblies using config files without the need of making changes to existing code.
Benefits of using Config based Validators:
- No need of source code for implementing the validation.
- No need to recompile after making changes to validation logic in config files.
- No need to change existing code.
I am ending up the Config based validation here. In coming articles, we will go deep into this Application block. I hope this article will be helpful for all.