文章分类

站点统计

  • 分类总数: 13 个
  • 文章总数: 145 篇
  • 评论总数: 35 条
  • 附件总数: 59 个
  • 建站日期: 2008-08-18
  • 访问总数: 388175 人次
  • RSS订阅: 文章|评论

Castle DynamicProxy介绍

Admin 于 2008-09-21 20:59:56 发表Castle

    java中有动态代理的概念,DotNet中没有,castle的DynamicProxy就是提供了类似于java动态代理的功能。动态代理是很多现代 软件技术的基础,例如AOP,现在有很多项目中采用了castle的DynamicProxy,他们包括:NHibernate (http://nhibernate.sourceforge.net/)、Retina.Net http://www.gotdotnet.com/workspaces /workspace.aspx?id=fd081831-5a33-45cd-9f23-a828dd1f3fd1)、iBatis.Net (http://www.sf.net/projects/ibatisnet)、Aspect# (http://aspectsharp.sf.net)、RhinoMocks (http://www.ayende.com/projects/rhino-mocks.aspx)

    这里介绍一下DynamicProxy的简单使用和基本原理。一个使用DynamicProxy的简单的例子:

  1. using System; 
  2. using Castle.DynamicProxy; 
  3.  
  4. namespace DynamicProxy 
  5.     class DynamicProxyTest 
  6.     { 
  7.         public static void Main() 
  8.         {            
  9.             ProxyGenerator generator = new ProxyGenerator(); 
  10.             object proxy = generator.CreateClassProxy(typeof(RealClass), new MyProxyInterceptor()); 
  11.             RealClass a = (proxy as RealClass); 
  12.             a.DoSomething(); 
  13.             Console.WriteLine(""); 
  14.             a.DoB(); 
  15.             Console.WriteLine(""); 
  16.             a.DoC(); 
  17.             Console.ReadLine(); 
  18.         }       
  19.     } 
  20.  
  21.    public class RealClass 
  22.     { 
  23.         public RealClass() { } 
  24.  
  25.         public virtual void DoSomething() 
  26.         { 
  27.             Console.WriteLine("DoSomething() in RealClass在运行。"); 
  28.         } 
  29.  
  30.         public virtual void DoB() 
  31.         { 
  32.             Console.WriteLine("DoB() in RealClass在运行。"); 
  33.         } 
  34.  
  35.         public void DoC() 
  36.         { 
  37.             Console.WriteLine("DoC() in RealClass在运行。"); 
  38.         } 
  39.     } 
  40.  
  41.     public class MyProxyInterceptor : StandardInterceptor 
  42.     { 
  43.         protected override void PreProceed(IInvocation invocation, params object[] args) 
  44.         { 
  45.             System.Console.WriteLine("PreProceed:" + invocation.Method.Name); 
  46.             base.PreProceed(invocation, args); 
  47.         } 
  48.  
  49.         protected override void PostProceed(IInvocation invocation, ref object returnValue, params object[] arguments) 
  50.         { 
  51.             if (invocation.Method.Name == "DoB"
  52.             { 
  53.                 Console.Out.WriteLine("PostProceed---DoB in ProxyInterceptor-- 在运行"); 
  54.             } 
  55.             else 
  56.             { 
  57.                 System.Console.WriteLine("PostProceed--{0} 在运行", invocation.Method.Name); 
  58.             } 
  59.             base.PostProceed(invocation, ref returnValue, arguments); 
  60.         } 
  61.     } 

运行结果:
PreProceed:DoSomething
DoSomething() in RealClass在运行。
PostProceed--DoSomething 在运行
PreProceed:DoB
DoB() in RealClass在运行。
PostProceed---DoB in ProxyInterceptor-- 在运行
DoC() in RealClass在运行。
     这个例子很简单,我们定义了一个需要动态代理的类RealClass,动态代理只要求该类需要代理的方法是虚方法。MyProxyInterceptor 是一个拦截器类,该类需要实现IInterceptor 接口,一般来说需要继承自StandardInterceptor类。通过查看StandardInterceptor源代码我们知道 StandardInterceptor是一个实现了模版方法模式的类。

  1. protected virtual void PreProceed(IInvocation invocation, params object[] args){} 
  2. protected virtual void PostProceed(IInvocation invocation, ref object returnValue, params object[] args){} 
  3. public virtual object Intercept(IInvocation invocation, params object[] args) 
  4.     PreProceed(invocation, args); 
  5.     object retValue = invocation.Proceed( args ); 
  6.     PostProceed(invocation, ref retValue, args); 
  7.     return retValue; 

     我们的具体拦截器类通过重载PreProceed(IInvocation invocation, params object[] args)和protected virtual void PostProceed(IInvocation invocation, ref object returnValue, params object[] args)方法,将我们需要在被代理方法执行之前和之后要添加的操作插入到方法执行过程中。其中IInvocation 封装了要代理的方法,returnValue是被代理方法可能存在的返回值,args是参数数组。我们的拦截器很简单,比如PreProceed只是 System.Console.WriteLine("PreProceed:" + invocation.Method.Name);然后调用父类base.PreProceed(invocation, args);PostProceed添加了一点功能,只是判断被代理的方法名是不是DoB,如果是 Console.Out.WriteLine("PostProceed---DoB in ProxyInterceptor-- 在运行");否则System.Console.WriteLine("PostProceed--{0} 在运行", invocation.Method.Name);然后调用base.PostProceed(invocation, ref returnValue, arguments);
从运行结果我们可以看出,对于RealClass的DoSomething和DoB方法,都有拦截器的输出,就是我们在MyProxyInterceptor中定义的。但DoC方法没有被代理,原因是DoC不是virtual方法。
最后看一下使用。

  1. ProxyGenerator generator = new ProxyGenerator();//ProxyGenerator 用于生成代理对象 
  2. object proxy = generator.CreateClassProxy(typeof(RealClass), new MyProxyInterceptor()); 
  3. //通过generator.CreateClassProxy方法动态生成代理类。 
  4. RealClass a = (proxy as RealClass);//生成的代理对象是RealClass 的子类,所以可以强制转换为RealClass 
  5. a.DoSomething();//调用代理实例的相应与被代理的相同的方法,如果该方法已经被代理,则执行代理后的方法。 
  6. Console.WriteLine(""); 
  7. a.DoB(); 
  8. Console.WriteLine(""); 
  9. a.DoC();//如果该方法没有被代理,则执行代理前的方法。 
  10. Console.ReadLine(); 

通 过generator.CreateClassProxy方法动态生成代理类将会把我们指定给要代理的类或接口的被代理方法封装入IInvocation 对象。并通过Intercept方法插入我们定义的PreProceed(invocation, args);和PostProceed(invocation, ref retValue, args);这些动态生成的类会直接按IL格式写入到动态代理类中,这样动态生成的动态代理类中就有类似下面的代码:

  1. public CProxyTypeRealClass0(IInterceptor interceptor1) 
  2. {   
  3.     this.__interceptor = interceptor1; 
  4. public override void DoSomething() 
  5.     IInvocation local0; 
  6.     object local1; 
  7.     object[] local2; 
  8.  
  9.     local0 = new SameClassInvocation(callable1, this, info1);//SameClassInvocation实现了IInvocation 
  10.     local2 = new Object[0]; 
  11.     local1 = this.__interceptor.Intercept(local0, local2);//.__interceptor是我们定义的MyProxyInterceptor 

其中SameClassInvocation继承自AbstractInvocation实现了IInvocation,其中的proceed方法如下:

  1. public virtual object Proceed(params object[] args) 
  2.     // If the user changed the target, we use reflection 
  3.     // otherwise the delegate will be used. 
  4.     if (changed_target == null
  5.     { 
  6.         return callable.Call( args );//callabel是动态生成的,封装了被代理的方法。 
  7.     } 
  8.     else 
  9.     { 
  10.         return Method.Invoke( changed_target, args ); 
  11.     } 

简 单的说就是,通过ProxyGenerator生成类的代理后,所产生的代理类就是CProxyTypeRealClass0,当调用 DoSomething方法时,就将我们在MyProxyInterceptor定义的方法插入到被代理对象方法DoSomething的执行前后,从而 实现了对方法的动态代理。当然这种代理职能对方法执行前后进行拦截,要实现根复杂的拦截,需要AOP。

被阅877次, 0票Castle DynamicProxy 发表评论
1 / 1 / 1 | « 1 » |
Powered by MiniBoke v2.0.0.8 Build 0828

Copyright © 2008 开源吧!. All rights reserved.

粤ICP备07500939号