Posts Tagged ‘PIAB’

Policy Injection Application Block

Monday, December 29th, 2008

Here is how Microsoft describes it: “The Policy Injection Application Block provides a mechanism for automatically applying policies to object instances; this helps developers to better manage crosscutting concerns and maximize separation of concerns and encapsulation of behavior. Developers define the set of policies for the target classes and their members through configuration of the Policy Injection Application Block or by applying attributes to individual members of the target class.

The matching rules for each policy encompass a range of capabilities for selecting classes and members, including the name of the assembly, the namespace, the type, the member name, the member signature, attributes that are applied to the member, and more.

I’ll show you one small demo apllication which demonstrates how it works:
Create empty solution and add Class Library project called: BusinessObject. put those 2 files there: CellPhone.cs and ICellPhone.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace BusinessObject
{
    public interface ICellPhone
    {
        void SendSMS(String toPhoneMumber, String mesgSubject, String text);
        void Ring();
        void Dial(String toPhoneNumber);
        void Answer();
        void HangUp();
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace BusinessObject
{
    public class CellPhone : ICellPhone
    {
        private String phoneNumber;

        public CellPhone(String number)
        {
            phoneNumber = number;
        }

        public void SendSMS(String toPhoneMumber, String mesgSubject, String text)
        {
            Console.WriteLine("I am sending SMS to "
                + toPhoneMumber
                + ", with subject "
                + mesgSubject
                + " and text "
                + text
                );
        }
        public void Ring()
        {
            Console.WriteLine("Ringing ...");
        }

        public void Dial(String toPhoneNumber)
        {
            Console.WriteLine("I am dialing " + toPhoneNumber);
        }

        public void Answer()
        {
            Console.WriteLine("Yes plese!");
        }

        public void HangUp()
        {
            Console.WriteLine("Have a nice day.");
        }

    }
}

As you can see we have very simple class CellPhone which implements ICellPhone interface. Then, create and new project to your solution. It can be Console Application. I’ll call it client.
Add BusinessObject reference to your client project. Also you have to add some .NET references in order to use Policy Injection.
Microsoft.Practices.EnterpriseLibrary.Common
Microsoft.Practices.EnterpriseLibrary.PolicyInjection
Microsoft.Practices.EnterpriseLibrary.PolicyInjection
Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers

Now take a look of code for Program.cs

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.Logging;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            BusinessObject.CellPhone cellPhone = PolicyInjection.Create("222333");
            cellPhone.Dial("123123");
            cellPhone.Ring();
            cellPhone.Answer();
        }
    }
}

This line is interesting:
BusinessObject.CellPhone cellPhone = PolicyInjection.Create(”222333″);
I don’t use BusinessObject.CellPhone cellPhone = new BusinessObject.CellPhone(”222333″); as you can see. If you want to use Policy Injection, this is how you create your objects. And very important rule: your object must implement some interface or must inherits class: “MarshalByRefObject“. Now you can try to “Build” and start (F5) your application, but for now there is no some differences. It works as it should be.
We have to do 2 more things. Add new project type of class library and to add App.config file to our client application.
First, add new project, type of class library, called: “CustomPIABHandler”.
Rename Class1.cs to LogCallHandler.cs and add some references to project as it is shown on the pictire below:





Now select custom handler. Click on property for Custom Handler and click type. From new PopUp window click on button “Load Assembly” and select LogCallHandler. You should navigate to your bin folder of your CustomPiabHanlder project and select CustomPIABHandler.dll.

And at last, add value for namespace matching rule. Click on Namespace Matching Rule and select it’s Property. Click on collection and add value: BusinessObject. And for the end add Reference of CustomPIABHandler to Client project.

to be continued

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in