您好,登錄后才能下訂單哦!
AOP介紹
面向切面編程(Aspect Oriented Programming,英文縮寫為AOP),通過預(yù)編譯方式和運行期動態(tài)代理實現(xiàn)程序功能的統(tǒng)一維護的一種技術(shù)。
AOP是OOP的延續(xù),是軟件開發(fā)中的一個熱點.
常用于:
Authentication
Caching
Lazy loading
Transactions
普通類
1 2 3 4 5 6 7 8 9 |
|
代理類
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
執(zhí)行
1 2 3 4 5 6 7 8 9 10 |
|
AOP有動態(tài)代理和靜態(tài)IL織入.
本節(jié)主要介紹動態(tài)代理方式,靜態(tài)可參考PostSharp.
原理:本質(zhì)是創(chuàng)建繼承原來類的代理類.重寫虛方法實現(xiàn)AOP功能.
只需引用:
Install-Package Castle.Core
(在Castle的2.5以上版本,已經(jīng)將 Castle.DynamicProxy2.dll 里有內(nèi)容,集成到 Castle.Core.dll 中。)
Simple Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
interceptor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
特性式AOP
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
1 2 3 4 5 6 7 8 9 |
|
1 2 3 4 5 6 7 8 9 10 11 |
|
非侵入式AOP
1 2 3 4 5 6 7 8 9 10 11 12 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Install-Package Autofac.Aop
通過特性標(biāo)簽綁定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
啟用攔截器執(zhí)行
1 2 3 4 5 6 7 8 9 10 11 |
|
或采用非侵入性方法(去掉class上的特性仍可以)
1 2 3 4 5 6 7 8 9 10 11 |
|
Unity默認(rèn)提供了三種攔截器:TransparentProxyInterceptor、InterfaceInterceptor、VirtualMethodInterceptor。
TransparentProxyInterceptor:代理實現(xiàn)基于.NET Remoting技術(shù),它可攔截對象的所有函數(shù)。缺點是被攔截類型必須派生于MarshalByRefObject。
InterfaceInterceptor:只能對一個接口做攔截,好處時只要目標(biāo)類型實現(xiàn)了指定接口就可以攔截。
VirtualMethodInterceptor:對virtual函數(shù)進行攔截。缺點是如果被攔截類型沒有virtual函數(shù)則無法攔截,這個時候如果類型實現(xiàn)了某個特定接口可以改用
Install-Package Unity.Interception
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。