惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
博客园 - 叶小钗
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
量子位
博客园 - 三生石上(FineUI控件)
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
人人都是产品经理
人人都是产品经理
V
Visual Studio Blog
Jina AI
Jina AI
L
LangChain Blog
M
MIT News - Artificial intelligence
MongoDB | Blog
MongoDB | Blog
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学

博客园 - 无名365

.NET 2.0 JSON Parser Provisioning 是什么意思? SOA与云计算 Good understand on WCF Data Contract & Serialization WCF DataContract EmitDefaultValue a xml configuration for unity and interception Programmatically Compress and Decompress Files using c# CodeRun Studio - A free cloud develop and debug service for .Net Cloud IDE 10个最“优秀”的代码注释 Compress and Decompress JQuery Mobile 1.0 Released, Gets Mixed Reaction Interceptor in Unity and Policy Injection in Unity Unity Configuration Node Unity Configuration from Separate Configuration File IoC and Unity - Configuration Objects and the Art of Data Modeling AppFabric Cache - Cache Cluster EF - How to delete an object without retrieving it LINQ to SQL Extension: Batch Deletion with Lambda Expression
Configuring Unity Container with Policy Injection Configu...
无名365 · 2011-11-22 · via 博客园 - 无名365

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

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.