阅读559 返回首页    go 阿里云 go 技术社区[云栖]


Enterprise Library深入解析与灵活应用(1):通过Unity Extension实现和Policy Injection Application Block的集成

 

   1: namespace Artech.PolicyInjectionIntegratedInUnity
   2: {
   3:     public class PolicyInjectionStrategy : EnterpriseLibraryBuilderStrategy
   4:     {
   5:         public override void PreBuildUp(IBuilderContext context)
   6:         {
   7:             base.PreBuildUp(context);
   8:             if (context.Policies.Get<IPolicyInjectionPolicy>(context.BuildKey) == null)
   9:             {
  10:                 context.Policies.Set<IPolicyInjectionPolicy>(new PolicyInjectionPolicy(true), context.BuildKey);
  11:             }
  12:         } 
  13:  
  14:         public override void PostBuildUp(IBuilderContext context)
  15:         {
  16:             base.PostBuildUp(context);
  17:             IPolicyInjectionPolicy policy = context.Policies.Get<IPolicyInjectionPolicy>(context.BuildKey);
  18:             if ((policy != null) && policy.ApplyPolicies)
  19:             {
  20:                 policy.SetPolicyConfigurationSource(EnterpriseLibraryBuilderStrategy.GetConfigurationSource(context));
  21:                 context.Existing = policy.ApplyProxy(context.Existing, BuildKey.GetType(context.OriginalBuildKey));
  22:             }
  23:         }
  24:     }
  25: }
  26:  
  27:  

注:PolicyInjectionPolicy定了在Microsoft.Practices.EnterpriseLibrary.PolicyInjection dll中,实现了Microsoft.Practices.EnterpriseLibrary.PolicyInjection.ObjectBuilder.IPolicyInjectionPolicy和Microsoft.Practices.ObjectBuilder2.IBuilderPolicy。其ApplyProxy方法实际上就是调用了PolicyInjecctor的Wrap方法。

   1: namespace Artech.PolicyInjectionIntegratedInUnity
   2: {
   3:     public class PolicyInjectionExtension : UnityContainerExtension
   4:     {
   5:         protected override void Initialize()
   6:         {
   7:             base.Context.Strategies.AddNew<PolicyInjectionStrategy>(UnityBuildStage.Initialization);
   8:         }
   9:     }
  10: } 
  11:  

   1: public interface ITimeService
   2: {
   3:         DateTime GetSystemTime();
   4: } 
   5:  
   6: [CachingCallHandler]
   7: public class TimeService : ITimeService
   8: {
   9:     #region IContract Members 
  10:  
  11:     public DateTime GetSystemTime()
  12:     {
  13:             return DateTime.Now;
  14:     } 
  15:  
  16:         #endregion
  17:     }
  18: } 
  19:  

   1: namespace Artech.PolicyInjectionIntegratedInUnity
   2: {
   3:     class Program
   4:     {
   5:         static void Main(string[] args)
   6:         {
   7:                 IUnityContainer container = new UnityContainer();
   8:                 container.AddExtension(new PolicyInjectionExtension());
   9:                      container.RegisterType<ITimeService, TimeService>();
  10:                 ITimeService instance = container.Resolve<ITimeService>();
  11:                 for (int i = 0; i < 10; i++)
  12:                 {
  13:                     Console.WriteLine(instance.GetSystemTime());
  14:                     Thread.Sleep(1000);
  15:                 }
  16:             }        
  17:     }  
  18: } 
  19:  

image

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <configuration>
   3:     <configSections>
   4:         <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
   5:     </configSections>   
   6:     <unity>
   7:         <containers>
   8:             <container>
   9:                 <types>
  10:                    <type type="Artech.PolicyInjectionIntegratedInUnity.ITimeService,Artech.PolicyInjectionIntegratedInUnity" mapTo="Artech.PolicyInjectionIntegratedInUnity.TimeService,Artech.PolicyInjectionIntegratedInUnity"/>
  11:                 </types>
  12:                 <extensions>
  13:                     <add type="Artech.PolicyInjectionIntegratedInUnity.PolicyInjectionExtension,Artech.PolicyInjectionIntegratedInUnity" />
  14:                 </extensions>
  15:             </container>
  16:         </containers>
  17:     </unity>
  18: </configuration> 
  19:  

   1: namespace Artech.PolicyInjectionIntegratedInUnity
   2: {
   3:     class Program
   4:     {
   5:         static void Main(string[] args)
   6:         {
   7:             IUnityContainer container = new UnityContainer();
   8:             UnityConfigurationSection unityConfigSection = ConfigurationManager.GetSection("unity") as UnityConfigurationSection;
   9:             unityConfigSection.Containers.Default.Configure(container);
  10:             ITimeService instance = container.Resolve<ITimeService>();
  11:             for (int i = 0; i < 10; i++)
  12:             {
  13:                 Console.WriteLine(instance.GetSystemTime());
  14:                 Thread.Sleep(1000);
  15:             }
  16:         }
  17:     } 
  18: } 
  19:  

image

 

 

 

 

 

 


 

Enterprise Library深入解析与灵活应用:
[1] 通过Unity Extension实现和Policy Injection Application Block的集成
[2] 通过SqlDependency实现Cache和Database的同步
[3] 倘若将Unity、PIAB、Exception Handling引入MVP模式
[4] 创建一个自定义Exception Handler改变ELAB的异常处理机制
[5] 创建一个简易版的批处理执行器,认识Enterprise Library典型的配置方式和对象创建方式
[6] 自己动手创建迷你版AOP框架
[7] 再谈PIAB与Unity之间的集成



作者:蒋金楠
微信公众账号:大内老A
微博:www.weibo.com/artech
如果你想及时得到个人撰写文章以及着作的消息推送,或者想看看个人推荐的技术资料,可以扫描左边二维码(或者长按识别二维码)关注个人公众号(原来公众帐号蒋金楠的自媒体将会停用)。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文链接

最后更新:2017-10-30 17:04:30

  上一篇:go  WCF后续之旅(10): 通过WCF Extension实现以对象池的方式创建Service Instance
  下一篇:go  《EnterLib PIAB深入剖析》系列博文汇总