代码改变世界

Configuring Unity Container with Policy Injection Configuration

2011-11-22 13:45  无名365  阅读(223)  评论(0编辑  收藏  举报

Recently while preparing a presentation on Policy Injection Application block integration with Unity, I struggled with configuring the unity container to understand for PIAB configuration. Hence, thought of writing it here. This blog post talks about, How to configure Unity container to understand PIAB configuration(Policies, Call Handlers, Maching Rules) so that, we can utilize the Policy Based AOP provided by PIAB.

Below is the code from my sample, Here I am configuring a simple business object BOImplementation which implements IBOInterface.

IUnityContainer container = new UnityContainer();

 

            //Interception is provided as an extension to Unity, Hence need to add this extension to the container

            container.AddNewExtension<Interception>();

 

            //Get Policy Injection Settings from the Configuration

            IConfigurationSource configSource = ConfigurationSourceFactory.Create();

            PolicyInjectionSettings policyInjectionsettings = (PolicyInjectionSettings)configSource.GetSection(PolicyInjectionSettings.SectionName);

 

            //Register Type with the container

            container.RegisterType<IBOInterface, BOImplementation>();

 

            //Configure the container for Interception on given interface (IBOInterface in this case)

            //Here I am using TransparentProxyInterceptor, you can choose from

            //InterfaceInterceptor, TransparentProxyInterceptor or VirtualMethodInterceptor, depending upon the requirement

            container.Configure<Interception>().SetInterceptorFor<IBOInterface>(new TransparentProxyInterceptor());

 

 

            // Apply Polocy Injection Settings to the container

            if (policyInjectionsettings != null)

            {

                policyInjectionsettings.ConfigureContainer(container, configSource);

            }

Now, when I try to resolve the business object instance, I get a Transparent Proxy instead of a real object.

IBOInterface businessObject = container.Resolve<IBOInterface>();

businessObject is a transparent proxy, If there exists a policy which applies to  BOImplementation In the PIAB configuration. Hence the interception mechanism is in place to provide your call handler a chance to do the AOP for you.