动态代理

目录

背景公共代码5种代理模式第一种:ClassProxy第二种:ClassProxyWithTarget第三种:InterfaceProxyWithoutTarget第四种:InterfaceProxyWithTarget第五种:InterfaceProxyWithTargetInterface1种Mixin模式动态代理在DCI中的应用经常见到的动态代理场景备注

背景返回目录

什么叫“动态代理”,代理模式我们都知道,动态代理就是动态生成的代理(采用Emit)。

重量级的ORM和IOC产品离不开动态代理,作为开发人员,多数情况不用关注动态代理的内部实现机制,但是了解其一般的规律和模式还是有必要的,比如:虽然你开发期间采用了POCO,因为开启了动态代理,运行期间则不是POCO。

本文简单描述了5种代理生成模式和1种Mixin模式,最后给出一个示例。

公共代码返回目录

这里先给出公共代码。

复制代码
 1     public interface IPlayable
 2     {
 3         void Play();
 4     }
 5 
 6     public class Animal : IPlayable
 7     {
 8         public virtual void Play()
 9         {
10             Console.WriteLine("Animal.Play");
11         }
12     }
13 
14     public class Dog : Animal
15     {
16         public override void Play()
17         {
18             Console.WriteLine("Dog.Play");
19         }
20     }
21 
22     public interface IRunable
23     {
24         void Run();
25     }
26 
27     public class RunAbility : IRunable
28     {
29         public void Run()
30         {
31             Console.WriteLine("RunAbility.Run");
32         }
33     }
34 
35     public class AnimalInterceptor : IInterceptor
36     {
37         public void Intercept(IInvocation invocation)
38         {
39             Console.WriteLine("Before AnimalInterceptor.Intercept");
40             if (invocation.InvocationTarget != null)
41             {
42                 invocation.Proceed();
43             }
44             Console.WriteLine("After AnimalInterceptor.Intercept");
45         }
46     }
复制代码

5种代理模式返回目录

第一种:ClassProxy返回目录

代码示例

复制代码
 1             {
 2                 Console.WriteLine("
*************ClassProxy*************
");
 3                 var generator = new ProxyGenerator();
 4                 var animal = generator.CreateClassProxy<Animal>(new AnimalInterceptor());
 5                 animal.Play();
 6 
 7                 Console.WriteLine(animal.GetType());
 8                 Console.WriteLine(animal.GetType().BaseType);
 9 
10                 var compositeField = animal.GetType().GetField("__target");
11                 Console.WriteLine(compositeField);
12 
13                 foreach (var interfaceType in animal.GetType().GetInterfaces())
14                 {
15                     Console.WriteLine(interfaceType);
16                 }
17             }
复制代码

运行结果

动态代理类图

等待上传中。

第二种:ClassProxyWithTarget返回目录

代码示例

复制代码
 1             {
 2                 Console.WriteLine("
*************ClassProxyWithTarget*************
");
 3                 var generator = new ProxyGenerator();
 4                 var animal = generator.CreateClassProxyWithTarget<Animal>(new Dog(), new AnimalInterceptor());
 5                 animal.Play();
 6 
 7                 Console.WriteLine(animal.GetType());
 8                 Console.WriteLine(animal.GetType().BaseType);
 9 
10                 var compositeField = animal.GetType().GetField("__target");
11                 Console.WriteLine(compositeField);
12 
13                 foreach (var interfaceType in animal.GetType().GetInterfaces())
14                 {
15                     Console.WriteLine(interfaceType);
16                 }
17             }
复制代码

运行结果

动态代理类图

等待上传中。

第三种:InterfaceProxyWithoutTarget返回目录

代码示例

复制代码
 1             {
 2                 Console.WriteLine("
*************InterfaceProxyWithoutTarget*************
");
 3                 var generator = new ProxyGenerator();
 4                 var animal = generator.CreateInterfaceProxyWithoutTarget<IPlayable>(new AnimalInterceptor());
 5                 animal.Play();
 6 
 7                 Console.WriteLine(animal.GetType());
 8                 Console.WriteLine(animal.GetType().BaseType);
 9 
10                 var compositeField = animal.GetType().GetField("__target");
11                 Console.WriteLine(compositeField);
12 
13                 foreach (var interfaceType in animal.GetType().GetInterfaces())
14                 {
15                     Console.WriteLine(interfaceType);
16                 }
17             }
复制代码

运行结果

动态代理类图

等待上传中。

第四种:InterfaceProxyWithTarget返回目录

测试代码

复制代码
 1             {
 2                 Console.WriteLine("
*************InterfaceProxyWithTarget*************
");
 3                 var generator = new ProxyGenerator();
 4                 var animal = generator.CreateInterfaceProxyWithTarget<IPlayable>(new Dog(), new AnimalInterceptor());
 5                 animal.Play();
 6 
 7                 Console.WriteLine(animal.GetType());
 8                 Console.WriteLine(animal.GetType().BaseType);
 9 
10                 var compositeField = animal.GetType().GetField("__target");
11                 Console.WriteLine(compositeField);
12 
13                 foreach (var interfaceType in animal.GetType().GetInterfaces())
14                 {
15                     Console.WriteLine(interfaceType);
16                 }
17             }
复制代码

运行结果

动态代理类图

等待上传中。

第五种:InterfaceProxyWithTargetInterface返回目录

测试代码

复制代码
 1             {
 2                 Console.WriteLine("
*************InterfaceProxyWithTargetInterface*************
");
 3                 var generator = new ProxyGenerator();
 4                 var animal = generator.CreateInterfaceProxyWithTargetInterface<IPlayable>(new Dog(), new AnimalInterceptor());
 5                 animal.Play();
 6 
 7                 Console.WriteLine(animal.GetType());
 8                 Console.WriteLine(animal.GetType().BaseType);
 9 
10                 var compositeField = animal.GetType().GetField("__target");
11                 Console.WriteLine(compositeField);
12 
13                 foreach (var interfaceType in animal.GetType().GetInterfaces())
14                 {
15                     Console.WriteLine(interfaceType);
16                 }
17             }
复制代码

运行结果

动态代理类图

等待上传中。

1种Mixin模式返回目录

测试代码

复制代码
 1             {
 2                 Console.WriteLine("
*************Mixin*************
");
 3                 var generator = new ProxyGenerator();
 4                 var options = new ProxyGenerationOptions();
 5                 options.AddMixinInstance(new RunAbility());
 6                 var animal = generator.CreateClassProxy<Animal>(options, new AnimalInterceptor());
 7                 animal.Play();
 8                 (animal as IRunable).Run();
 9 
10                 Console.WriteLine(animal.GetType());
11                 Console.WriteLine(animal.GetType().BaseType);
12 
13                 var compositeField = animal.GetType().GetField("__target");
14                 Console.WriteLine(compositeField);
15 
16                 foreach (var field in animal.GetType().GetFields())
17                 {
18                     if (field.Name.StartsWith("__mixin"))
19                     {
20                         Console.WriteLine(field);
21                     }
22                 }
23 
24                 foreach (var interfaceType in animal.GetType().GetInterfaces())
25                 {
26                     Console.WriteLine(interfaceType);
27                 }
28             }
复制代码

运行结果

动态代理类图

等待上传中。

动态代理在DCI中的应用返回目录

参考链接:http://www.cnblogs.com/happyframework/archive/2013/04/25/3040461.html#content_4

经常见到的动态代理场景返回目录

  1. ORM延时加载。
  2. AOP拦截(不全是使用的动态代理,有的使用透明代理或字节码增强,有的使用平台自带的管道过滤器,如:ASP.NET MVC的FilterAction)。
  3. WCF客户端代理。

备注返回目录

了解了这些模式后,自己开发一个简单的动态代理模式应该不是问题了,如果是C#语言,得学好Emit(不是那么简单),如果是Ruby的话,估计就非常Easy了,找个机会给出这两种语言的不同实现。

 
分类: .NET
原文地址:https://www.cnblogs.com/Leo_wl/p/3296137.html