自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Castle.DynamicProxy在iBATIS.NET中的使用

開發(fā) 后端
在iBATIS.NET中通過Castle.DynamicProxy是如何實現(xiàn)動態(tài)代理的功能的呢?本文就向你介紹這些相關(guān)信息。

Castle是另外一個框架,包含了AOP、IOC、ORM等多個方面,其中的Castle.DynamicProxy可以實現(xiàn)動態(tài)代理的功能,這個也是很多框架的基礎(chǔ)。在IBatis.Net中就是使用了Castle.DynamicProxy來實現(xiàn)數(shù)據(jù)庫連接等動態(tài)操作的。同時在NHibernet等其他框架中也使用到了這個技術(shù)。

下面我通過一個簡單例子來看一下如何在我們的代碼中調(diào)用Castle.DynamicProxy:

一般情況下要有三個類:

1、接口類:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.  
  5. namespace GSpring.CastleTest  
  6. {  
  7.     public interface ITest  
  8.     {  
  9.         string GetName(string pre);  
  10.     }  

2、實現(xiàn)類:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.  
  5. namespace GSpring.CastleTest  
  6. {  
  7.     public class Test : ITest  
  8.     {  
  9.         public string GetName(string pre)  
  10.         {  
  11.             return pre + ",GSpring";  
  12.         }  
  13.     }  

這兩個都很普通的接口和實現(xiàn)

3、代理類:

  1. using System;  
  2. using System.Collections;  
  3. using System.Reflection;  
  4. using Castle.DynamicProxy;  
  5.  
  6. namespace GSpring.CastleTest  
  7. {  
  8.     /**//// <summary>  
  9.     /// Summary description for DaoProxy.  
  10.     /// </summary>  
  11.     public class InterceptorProxy : IInterceptor  
  12.     {  
  13.          public object Intercept(IInvocation invocation, params object[] arguments)  
  14.         {  
  15.             Object result = null;  
  16.  
  17.             //這里可以進行數(shù)據(jù)庫連接、打日志、異常處理、權(quán)限判斷等共通操作  
  18.             result = invocation.Proceed(arguments);  
  19.  
  20.             return result;  
  21.         }  
  22.  
  23.     }  

這個類首先實現(xiàn)接口IInterceptor,然后就可以在方法Intercept中加入我們自己的邏輯

然后看一下調(diào)用的方式:

  1. ProxyGenerator proxyGenerator = new ProxyGenerator();  
  2. IInterceptor handler = new InterceptorProxy();  
  3. Type[] interfaces = { typeof(ITest) };  
  4. Test test = new Test();  
  5. ITest iTest = (proxyGenerator.CreateProxy(interfaces, handler, test) as ITest);  
  6. string result = iTest.GetName("Hello"); 

最后一句調(diào)用的地方,實際會首先執(zhí)行InterceptorProxy類中的Intercept方法。

以上就是在iBATIS.NET中通過Castle.DynamicProxy實現(xiàn)動態(tài)代理的功能,希望對你有所幫助。

【編輯推薦】

  1. iBATIS.NET中的多表查詢方法淺析
  2. iBATIS.NET日志處理淺析
  3. iBATIS.NET字段映射自定義對象淺析
  4. iBATIS.NET中動態(tài)選擇DAO淺析
  5. iBATIS.NET連接數(shù)據(jù)庫處理淺析
責(zé)任編輯:仲衡 來源: cnblogs
相關(guān)推薦

2009-07-16 13:50:31

ibatisResultMap

2009-07-20 14:56:18

iBATIS.NET動態(tài)選擇DAO

2009-07-22 09:07:01

iBATIS.NET

2009-07-20 13:22:47

iBATIS.Net日

2009-07-20 10:06:07

iBATIS.net查詢方式

2009-07-20 09:27:42

IBATIS.netDAO

2009-07-21 17:06:35

iBATIS.NET執(zhí)

2009-07-22 14:28:52

iBATIS.NET配

2009-07-21 13:50:00

iBATIS.NET調(diào)

2009-07-21 14:15:00

iBATIS.NET多

2009-07-21 15:21:59

iBATIS.NET多

2009-07-22 14:11:09

配置ibatis.neiBatis.net配

2009-07-20 09:51:19

iBATIS.net數(shù)據(jù)庫緩存

2009-07-21 16:17:28

iBATIS.NET

2009-07-20 13:47:08

iBATIS.NET字

2009-07-17 17:57:20

NPetShop iBATIS.Net

2009-07-20 15:14:44

iBATIS.NET連

2011-03-15 13:30:27

IBatis.netMySQL

2009-07-21 16:30:15

iBATIS.NET與單元測試

2021-06-30 00:19:43

AOP動態(tài)代理
點贊
收藏

51CTO技術(shù)棧公眾號