Introduction to Enterprise Library - Part X

In previous article, we had discussed about implementation of custom validators using. VAB. Now, we look into integration of VAB with Policy Injection Application Block [PIAB]. PIAB is a collection of handlers for implementing common scenarios like caching, validation, logging etc easily. We will look deep into this application block in coming articles.

Create a new console application in VS 2008 and name it as VABAndPIABIntegration.

Add a reference to below Enterprise Library dlls present in its Installation folder:

Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.PolicyInjection.dll
Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.dll
Microsoft.Practices.EnterpriseLibrary.Validation.dll
Microsoft.Practices.Unity.Interception.dll

Add a new class Employee with below definition in Program.cs:

class Employee : MarshalByRefObject
    {
        private static List<Employee> empList = new List<Employee>();
        public int EmpID { get; set; }
        public string EmpName { get; set; }
        public double Salary { get; set; }
        public void AddEmployee(Employee emp)
        {
            empList.Add(emp);
        }
        [ValidationCallHandler]
public Employee GetEmpDetails([RangeValidator(1,RangeBoundaryType.Inclusive,100,RangeBoundaryType.Inclusive,MessageTemplate="Emp ID should be below 100.")] int empid)
        {
            foreach (Employee emp in empList)
            {
                if (emp.EmpID == empid)
                {
                    return emp;
                }
            }
            return null;
        }
    }

Here, we are implementing MarshalByRefObject class. In order to use PIAB, it's mandatory to implement MarshalByRefObject. Than, we add ValidationCallHandler attribute to GetEmpDetails method. When we add this attribute, it will intercept the calls to that method and validates the parameters. If validation fails, it will throw ArgumentValidationException and returns back without calling the method.

Now, we need to test this method. So, add the below code to Main method:

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

        static void Main(string[] args)
        {
            Employee e1 = PolicyInjection.Create<Employee>();
            e1.EmpID = 10;
            e1.EmpName = "Test";
            e1.Salary = 1000;
            e1.AddEmployee(e1);
            Employee e2 = PolicyInjection.Create<Employee>();
            e2.EmpID = 20;
            e2.EmpName = "CSharp";
            e2.Salary = 2000;
            e2.AddEmployee(e2);
            try
            {
                Employee e3 = e1.GetEmpDetails(150);
                if (e3 != null)
                {
                    Console.WriteLine("Emp ID" + e3.EmpID);
                    Console.WriteLine("Emp Name" + e3.EmpName);
                    Console.WriteLine("Employee Salary" + e3.Salary);
                }
            }
            catch (ArgumentValidationException ex)
            {
                foreach (ValidationResult rs in ex.ValidationResults)
                {
                    Console.WriteLine(rs.Message);
                }
            }
            Console.ReadLine();
        }
    }

After importing necessary namespaces, we are creating instances of Employee class using PolicyInjection's Create method. It's mandatory to use Create method for instance creation by which PIAB can intercept the calls. Now, run the application, the output will be like this:

in.gif

Finally, I will outline the steps of the Integration:

  • VAB can be used with PIAB to automatically validate parameters of a method.
  • Validation Rules can be specified within Parameter types or as attributes in the parameter definition.
  • Instances of the class should be created using PolicyInjection's Create method.
  • Validation handler can be applied using either attributes or Configuration.
  • If validation fails, an ArgumentValidationException will be thrown. This Exception will be having all validation results in it.

I am ending up this VAB series here. I am attaching source code for reference. In coming articles, we will go deep into another Application block. I hope this series helped all.


Similar Articles