AOP之PostSharp7-解决IOC 不能直接new问题,简化IOC开发和IOC对象LazyLoad

简介:

经过几节的postsharp基础和每节的一个应用实例,已经基本PostSharp应用的能力,PostSharp主要是简化我们的开发,让编译器时候给我注入重复疲劳代码。  

   在今天我们的demo是,关于ioc(控制反转)的问题,ioc框架我们都会从ioc容器中取得我们的ioc对象注入,所以我们不能直接new对象得到我们的实例,必须Resolve。我一直都是很懒得人,既然有了PostSharp就的好好利用起来。大部份ioc逻辑是从以前的一篇利用Attribute简化Unity框架IOC注入转过来的,注入支持自定义配置文件,我个人不喜欢把配置信息全部写在一个web.config/app.config中,也不喜欢el的写在一个外部配置文件中,我个人倾向于每个模块在一个不能的配置文件,并在模块中在区分container容易,所以特别写了每个单独配置文件的延时加载,缓存。代码也不多,先上菜品:

 
  1. View Code   
  2.  
  3. using System;   
  4. using System.Collections.Generic;   
  5. using System.Linq;   
  6. using System.Text;   
  7. using Microsoft.Practices.Unity;   
  8. using Microsoft.Practices.Unity.Configuration;   
  9. using Microsoft.Practices.Unity.InterceptionExtension;   
  10.  
  11. namespace PostSharpDemo   
  12. {   
  13.     [Serializable]   
  14.     public class IocUnityResolverAttribute : PostSharp.Aspects.LocationInterceptionAspect   
  15.     {   
  16.         private Dictionary<string, Microsoft.Practices.Unity.Configuration.UnityConfigurationSection> sectionCache = new Dictionary<string, Microsoft.Practices.Unity.Configuration.UnityConfigurationSection>();   
  17.         private static object lockObj = new object();   
  18.         public IocUnityResolverAttribute(string Container)   
  19.         {   
  20.             this.Container = Container;   
  21.         }   
  22.  
  23.         public string Container   
  24.         {   
  25.             get;   
  26.             set;   
  27.         }   
  28.  
  29.         public string ConfigFile   
  30.         {   
  31.             get;   
  32.             set;   
  33.         }   
  34.  
  35.         public string Name   
  36.         {   
  37.             get;   
  38.             set;   
  39.         }   
  40.  
  41.         public Microsoft.Practices.Unity.Configuration.UnityConfigurationSection GetUnityConfigurationSection()   
  42.         {   
  43.             if (!string.IsNullOrEmpty(this.ConfigFile))   
  44.             {   
  45.                 Microsoft.Practices.Unity.Configuration.UnityConfigurationSection section = null;   
  46.                 if (!sectionCache.ContainsKey(this.ConfigFile))   
  47.                 {   
  48.                     lock (lockObj)   
  49.                     {   
  50.                         if (!sectionCache.ContainsKey(this.ConfigFile))   
  51.                         {   
  52.                             var fileMap = new System.Configuration.ExeConfigurationFileMap { ExeConfigFilename = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, this.ConfigFile) };   
  53.                             System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, System.Configuration.ConfigurationUserLevel.None);   
  54.                             if (configuration == null)   
  55.                             {   
  56.                                 throw new Exception(string.Format("Unity配置{0}不正确;", this.ConfigFile));   
  57.                             }   
  58.                             section = configuration.GetSection(Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.SectionName) as Microsoft.Practices.Unity.Configuration.UnityConfigurationSection;   
  59.                             sectionCache.Add(this.ConfigFile, section);   
  60.                         }   
  61.                     }   
  62.                 }   
  63.                 return sectionCache[this.ConfigFile];   
  64.             }   
  65.  
  66.             return System.Configuration.ConfigurationManager.GetSection(Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.SectionName) as Microsoft.Practices.Unity.Configuration.UnityConfigurationSection;   
  67.         }   
  68.  
  69.         public override void OnGetValue(PostSharp.Aspects.LocationInterceptionArgs args)   
  70.         {   
  71.             var current = args.GetCurrentValue();   
  72.             if (current == null)   
  73.             {   
  74.                 var unitySection = this.GetUnityConfigurationSection();   
  75.                 if (unitySection != null)   
  76.                 {   
  77.                     var container = new Microsoft.Practices.Unity.UnityContainer().LoadConfiguration(unitySection, string.IsNullOrEmpty(Container) ? unitySection.Containers.Default.Name : Container);   
  78.                     var obj = string.IsNullOrEmpty(Name) ? container.Resolve(args.Location.LocationType) : container.Resolve(args.Location.LocationType, Name);   
  79.                     if (obj != null)   
  80.                     {   
  81.                         //var piabAtttr = obj.GetType().GetCustomAttributes(typeof(ELPolicyinjectionAttribute), falseas ELPolicyinjectionAttribute[];   
  82.                         //if (piabAtttr.Length > 0)   
  83.                         //{   
  84.                         //    obj = Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjection.Wrap(type, obj);   
  85.                         //}   
  86.                         args.Value = obj;   
  87.                         args.ProceedSetValue();   
  88.                     }   
  89.                 }   
  90.             }   
  91.             args.ProceedGetValue();   
  92.         }   
  93.  
  94.         public override bool CompileTimeValidate(PostSharp.Reflection.LocationInfo locationInfo)   
  95.         {   
  96.             var p = locationInfo.PropertyInfo;             
  97.             if (p != null)   
  98.             {                 
  99.                 var attrs = p.GetCustomAttributes(typeof(Microsoft.Practices.Unity.DependencyAttribute), trueas Microsoft.Practices.Unity.DependencyAttribute[];   
  100.                 if (attrs != null && attrs.Length > 0)   
  101.                 {   
  102.                     return true;   
  103.                 }   
  104.             }   
  105.             return false;   
  106.         }   
  107.     }   
  108. }  
  109. 复制代码 

测试:

 
  1. View Code   
  2.  
  3. [IocUnityResolver("IocUnityResolver", ConfigFile = "App1.config")]   
  4.    class Program   
  5.    {   
  6.        [Microsoft.Practices.Unity.Dependency()]          
  7.        public static IIocUnityResolverAttributeTest IocUnityResolverAttributeTest   
  8.        {   
  9.            get;   
  10.            private set;   
  11.  
  12.        }   
  13.        public static IIocUnityResolverAttributeTest IocUnityResolverAttributeTest2   
  14.        {   
  15.            get;   
  16.            private set;   
  17.  
  18.        }   
  19.  
  20.        static void Main(string[] args)   
  21.        {   
  22.  
  23. Program.IocUnityResolverAttributeTest.Test("test ioc unity!");   
  24.  
  25.         Console.Read();   
  26.     }  
  27. 复制代码 

效果:

另外多加一句在AOP之PostSharp初见-OnExceptionAspect这节我们系列开篇我们看了Postsharp的多播(),所以我们可以很轻松的应用于我们的程序集,类上加Attribute实现,但是这里必须要区分Location的的注入决策,所以这里重写的基类方法的CompileTimeValidate,在有EL Unity 中Microsoft.Practices.Unity.DependencyAttribute标签的location我们才会去植入。有了这些我们就很轻松的在各个类,程序集,命名空间引用。

看看我们上边的实例反编译效果,IocUnityResolverAttributeTest带有Microsoft.Practices.Unity.DependencyAttribute标签所以会植入,而IocUnityResolverAttributeTest2没有标签所以不会植入;

 

附件下载:demo




 本文转自 破狼 51CTO博客,原文链接:http://blog.51cto.com/whitewolfblog/835247,如需转载请自行联系原作者

相关文章
|
6月前
|
Java Spring
11Spring - 基于AspectJ的AOP开发(注解的方式)
11Spring - 基于AspectJ的AOP开发(注解的方式)
26 0
|
6月前
|
XML Java 数据格式
10Spring - 基于AspectJ的AOP开发 (XML的方式)
10Spring - 基于AspectJ的AOP开发 (XML的方式)
23 0
|
7月前
|
XML 监控 Java
注解IOC&AOP
扫描类下的注解,哪些包下的类需要使用IOC注解
65 0
|
2月前
|
Java 测试技术 开发者
探究 Spring Boot 的核心:IOC 和 AOP
Spring Boot 作为一种简化 Spring 应用开发的工具,继承了 Spring 框架的核心概念,其中最重要的是控制反转(IOC)和面向切面编程(AOP)。它们是 Spring 框架的基础,同时也深深植根于 Spring Boot 中。本文将讨论 IOC 和 AOP 的概念以及它们在 Spring Boot 中的应用。
59 4
|
7月前
|
XML Java 数据格式
【Spring】全面讲解IOC、AOP、注入方式、bean的生命周期、aop通知应用 spring与web容器整合
Spring是一个开源的轻量级Java应用开发框架,它提供了一种简单、高效、灵活的方式来构建企业级应用程序。Spring框架的核心特点是依赖注入(Dependency Injection)和面向切面编程(Aspect-Oriented Programming),它通过一组模块化的组件提供全面的支持,使开发人员能够快速搭建可扩展、可维护的应用。
|
4月前
|
安全 Java 数据库连接
Spring框架:IoC容器、AOP、事务管理等知识讲解梳理
Spring框架:IoC容器、AOP、事务管理等知识讲解梳理
55 1
|
4月前
|
设计模式 Java 程序员
深入理解spring的两大特性 ioc 和aop
深入理解spring的两大特性 ioc 和aop
51 0
|
4月前
|
缓存 Java Spring
Spring AOP中CGLIB代理对象增强通知执行原理
Spring AOP中CGLIB代理对象增强通知执行原理
37 0
|
6月前
|
XML Java 数据格式
简单聊聊Spring中的IOC和AOP
简单聊聊Spring中的IOC和AOP
21 0
|
6月前
|
XML 缓存 Java
Spring IOC和AOP
Spring IOC和AOP
24 0