IOC框架
- Unity:微軟patterns&practicest團隊開發的IOC依賴注入框架,支持AOP橫切關注點。
- MEF(Managed Extensibility Framework):是一個用來擴展.NET應用程序的框架,可開發插件系統。
- Spring.NET:依賴注入、面向方面編程(AOP)、數據訪問抽象,、以及ASP.NET集成。
- Autofac:最流行的依賴注入和IOC框架,輕量且高性能,對項目代碼幾乎無任何侵入性。
- Ninject:基於.NET輕量級開源的依賴注入IOC框架
AOP框架
- Castle
- Encase 是C#編寫開發的為.NET平台提供的AOP框架。Encase 獨特的提供了把方面(aspects)部署到運行時代碼,而其它AOP框架依賴配置文件的方式。這種部署方面(aspects)的方法幫助缺少經驗的開發人員提高開發效率。
- NKalore 是一款編程語言,它擴展了C#允許在.net平台使用AOP。NKalore的語法簡單、直觀,它的編譯器是基於Mono C#編譯器(MCS)。NKalore目前只能在命令行或#Develop內部使用。NKalore兼容公共語言規范CLS(Common Language Specification),它可以在任何.NET開發環境中使用,包括微軟的Visual Studio .NET。
- PostSharp 讀取.NET字節模塊,轉換成對象模型。讓插件分析和轉換這個模型並寫回到MSIL。PostSharp使開發程序分析應用程序容易得像分析代碼規則和設計模式,它使程序開發的思想變革為面向方面軟件開發(AOSD/AOD)思想。
- AspectDNG 的目標是為.NET開發人員提供簡單而功能強大的AOP-GAOP實現。它效仿Java下的開源工具AspectJ 和 Spoon,成熟程度也很接近它們。
- RAIL(Runtime Assembly Instrumentation Library) 開源項目可以在C#程序集加載和運行前進行處理控制調整和重新構建。C#在CLR中,我們已經能夠動態加載程序集並且獲得程序集中的類和方法,RAIL(Runtime Assembly Instrumentation Library)的出現填補了CLR處理過程中的一些空白。
- SetPoint是一款.NET框架下的全功能(full-featured)AOP引擎.它着重為稱為語義切點(semantic pointcuts)的定義依賴RDF/OWL的使用.它的功能為一個IL-level,highly dynamic weaver&LENDL,一個引人注目的定義語言、、、、、、
- DotNetAOP為 CLR language提供AOP 框架基礎屬性。
- NAop是一個DotNet下的AOP框架。
- AspectSharp是DotNet下的免費AOP框架,它以Dynamic Proxies和XML作為配置文件。
一個Castle的實現
//首先下載Castle.Windsor.dll //自定義Interceptor public class MyInterceptor : IInterceptor { public void Intercept(IInvocation invocation) { PreProceed(invocation); invocation.Proceed(); PostProceed(invocation); } public void PreProceed(IInvocation invocation) { Console.WriteLine("方法執行前"); } public void PostProceed(IInvocation invocation) { Console.WriteLine("方法執行后"); } } //用戶注冊接口和實現 public interface IUserProcessor { void RegUser(User user); } public class UserProcessor : IUserProcessor { public virtual void RegUser(User user) { Console.WriteLine("用戶已注冊。Name:{0},PassWord:{1}", user.Name, user.PassWord); } } //客戶端調用 public class Client { public static void Run() { try { ProxyGenerator generator = new ProxyGenerator(); MyInterceptor interceptor = new MyInterceptor(); UserProcessor userprocessor = generator.CreateClassProxy<UserProcessor>(interceptor); User user= new User() { Name = "lee", PassWord = "123123123123" }; userprocessor.RegUser(user); } catch (Exception ex) { throw ex; } } }
