AOP之PostSharp7-解决IOC 不能直接new问题,简化IOC开发和IOC对象LazyLoad
? ? 經(jīng)過幾節(jié)的postsharp基礎(chǔ)和每節(jié)的一個(gè)應(yīng)用實(shí)例,已經(jīng)基本PostSharp應(yīng)用的能力,PostSharp主要是簡化我們的開發(fā),讓編譯器時(shí)候給我注入重復(fù)疲勞代碼。??
?? 在今天我們的demo是,關(guān)于ioc(控制反轉(zhuǎn))的問題,ioc框架我們都會(huì)從ioc容器中取得我們的ioc對象注入,所以我們不能直接new對象得到我們的實(shí)例,必須Resolve。我一直都是很懶得人,既然有了PostSharp就的好好利用起來。大部份ioc邏輯是從以前的一篇利用Attribute簡化Unity框架IOC注入轉(zhuǎn)過來的,注入支持自定義配置文件,我個(gè)人不喜歡把配置信息全部寫在一個(gè)web.config/app.config中,也不喜歡el的寫在一個(gè)外部配置文件中,我個(gè)人傾向于每個(gè)模塊在一個(gè)不能的配置文件,并在模塊中在區(qū)分container容易,所以特別寫了每個(gè)單獨(dú)配置文件的延時(shí)加載,緩存。代碼也不多,先上菜品:
using?System;?using?System.Collections.Generic;?
using?System.Linq;?
using?System.Text;?
using?Microsoft.Practices.Unity;?
using?Microsoft.Practices.Unity.Configuration;?
using?Microsoft.Practices.Unity.InterceptionExtension;?
namespace?PostSharpDemo?
{?
????[Serializable]?
????public?class?IocUnityResolverAttribute?:?PostSharp.Aspects.LocationInterceptionAspect?
????{?
????????private?Dictionary<string,?Microsoft.Practices.Unity.Configuration.UnityConfigurationSection>?sectionCache?=?new?Dictionary<string,?Microsoft.Practices.Unity.Configuration.UnityConfigurationSection>();?
????????private?static?object?lockObj?=?new?object();?
????????public?IocUnityResolverAttribute(string?Container)?
????????{?
????????????this.Container?=?Container;?
????????}?
????????public?string?Container?
????????{?
????????????get;?
????????????set;?
????????}?
????????public?string?ConfigFile?
????????{?
????????????get;?
????????????set;?
????????}?
????????public?string?Name?
????????{?
????????????get;?
????????????set;?
????????}?
????????public?Microsoft.Practices.Unity.Configuration.UnityConfigurationSection?GetUnityConfigurationSection()?
????????{?
????????????if?(!string.IsNullOrEmpty(this.ConfigFile))?
????????????{?
????????????????Microsoft.Practices.Unity.Configuration.UnityConfigurationSection?section?=?null;?
????????????????if?(!sectionCache.ContainsKey(this.ConfigFile))?
????????????????{?
????????????????????lock?(lockObj)?
????????????????????{?
????????????????????????if?(!sectionCache.ContainsKey(this.ConfigFile))?
????????????????????????{?
????????????????????????????var?fileMap?=?new?System.Configuration.ExeConfigurationFileMap?{?ExeConfigFilename?=?System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,?this.ConfigFile)?};?
????????????????????????????System.Configuration.Configuration?configuration?=?System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap,?System.Configuration.ConfigurationUserLevel.None);?
????????????????????????????if?(configuration?==?null)?
????????????????????????????{?
????????????????????????????????throw?new?Exception(string.Format("Unity配置{0}不正確;",?this.ConfigFile));?
????????????????????????????}?
????????????????????????????section?=?configuration.GetSection(Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.SectionName)?as?Microsoft.Practices.Unity.Configuration.UnityConfigurationSection;?
????????????????????????????sectionCache.Add(this.ConfigFile,?section);?
????????????????????????}?
????????????????????}?
????????????????}?
????????????????return?sectionCache[this.ConfigFile];?
????????????}?
????????????return?System.Configuration.ConfigurationManager.GetSection(Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.SectionName)?as?Microsoft.Practices.Unity.Configuration.UnityConfigurationSection;?
????????}?
????????public?override?void?OnGetValue(PostSharp.Aspects.LocationInterceptionArgs?args)?
????????{?
????????????var?current?=?args.GetCurrentValue();?
????????????if?(current?==?null)?
????????????{?
????????????????var?unitySection?=?this.GetUnityConfigurationSection();?
????????????????if?(unitySection?!=?null)?
????????????????{?
????????????????????var?container?=?new?Microsoft.Practices.Unity.UnityContainer().LoadConfiguration(unitySection,?string.IsNullOrEmpty(Container)???unitySection.Containers.Default.Name?:?Container);?
????????????????????var?obj?=?string.IsNullOrEmpty(Name)???container.Resolve(args.Location.LocationType)?:?container.Resolve(args.Location.LocationType,?Name);?
????????????????????if?(obj?!=?null)?
????????????????????{?
????????????????????????//var?piabAtttr?=?obj.GetType().GetCustomAttributes(typeof(ELPolicyinjectionAttribute),?false)?as?ELPolicyinjectionAttribute[];?
????????????????????????//if?(piabAtttr.Length?>?0)?
????????????????????????//{?
????????????????????????//????obj?=?Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjection.Wrap(type,?obj);?
????????????????????????//}?
????????????????????????args.Value?=?obj;?
????????????????????????args.ProceedSetValue();?
????????????????????}?
????????????????}?
????????????}?
????????????args.ProceedGetValue();?
????????}?
????????public?override?bool?CompileTimeValidate(PostSharp.Reflection.LocationInfo?locationInfo)?
????????{?
????????????var?p?=?locationInfo.PropertyInfo;???????????
????????????if?(p?!=?null)?
????????????{???????????????
????????????????var?attrs?=?p.GetCustomAttributes(typeof(Microsoft.Practices.Unity.DependencyAttribute),?true)?as?Microsoft.Practices.Unity.DependencyAttribute[];?
????????????????if?(attrs?!=?null?&&?attrs.Length?>?0)?
????????????????{?
????????????????????return?true;?
????????????????}?
????????????}?
????????????return?false;?
????????}?
????}?
} 測試: [IocUnityResolver("IocUnityResolver",?ConfigFile?=?"App1.config")]?
???class?Program?
???{?
???????[Microsoft.Practices.Unity.Dependency()]????????
???????public?static?IIocUnityResolverAttributeTest?IocUnityResolverAttributeTest?
???????{?
???????????get;?
???????????private?set;?
???????}?
???????public?static?IIocUnityResolverAttributeTest?IocUnityResolverAttributeTest2?
???????{?
???????????get;?
???????????private?set;?
???????}?
???????static?void?Main(string[]?args)?
???????{?
Program.IocUnityResolverAttributeTest.Test("test?ioc?unity!");?
????????Console.Read();?
????}
效果:
另外多加一句在AOP之PostSharp初見-OnExceptionAspect這節(jié)我們系列開篇我們看了Postsharp的多播(),所以我們可以很輕松的應(yīng)用于我們的程序集,類上加Attribute實(shí)現(xiàn),但是這里必須要區(qū)分Location的的注入決策,所以這里重寫的基類方法的CompileTimeValidate,在有EL Unity 中Microsoft.Practices.Unity.DependencyAttribute標(biāo)簽的location我們才會(huì)去植入。有了這些我們就很輕松的在各個(gè)類,程序集,命名空間引用。
看看我們上邊的實(shí)例反編譯效果,IocUnityResolverAttributeTest帶有Microsoft.Practices.Unity.DependencyAttribute標(biāo)簽所以會(huì)植入,而IocUnityResolverAttributeTest2沒有標(biāo)簽所以不會(huì)植入;
?
附件下載:demo
AOP之PostSharp初見-OnExceptionAspect AOP之PostSharp2-OnMethodBoundaryAspect AOP之PostSharp3-MethodInterceptionAspect AOP之PostSharp4-實(shí)現(xiàn)類INotifyPropertyChanged植入 AOP之PostSharp5-LocationInterceptionAspect AOP之PostSharp6-EventInterceptionAspect AOP之PostSharp7-解決IOC 不能直接new問題,簡化IOC開發(fā)和IOC對象LazyLoad http://www.cnblogs.com/whitewolf/category/312638.html?
作者:破??狼?
出處:http://www.cnblogs.com/whitewolf/?
本文版權(quán)歸作者,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。該文章也同時(shí)發(fā)布在我的獨(dú)立博客中-個(gè)人獨(dú)立博客、博客園--破狼和51CTO--破狼。http://www.cnblogs.com/whitewolf/archive/2011/12/18/PostSharp7.html
總結(jié)
以上是生活随笔為你收集整理的AOP之PostSharp7-解决IOC 不能直接new问题,简化IOC开发和IOC对象LazyLoad的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 认识和入门 WebRTC
- 下一篇: 第三章 经典场景设计