Castle是另外一个框架,包含了AOP、IOC、ORM等多个方面,其中的Castle.DynamicProxy可以实现动态代理的功能,这个也是很多框架的基础。在IBatis.Net中就是使用了Castle.DynamicProxy来实现数据库连接等动态操作的。同时在NHibernet等其他框架中也使用到了这个技术。
下面我通过一个简单例子来看一下如何在我们的代码中调用Castle.DynamicProxy:
一般情况下要有三个类:
1、接口类:
IBatis.Net学习笔记(六):Castle.DynamicProxy的使用using System;
IBatis.Net学习笔记(六):Castle.DynamicProxy的使用
using System.Collections.Generic;
IBatis.Net学习笔记(六):Castle.DynamicProxy的使用
using System.Text;
IBatis.Net学习笔记(六):Castle.DynamicProxy的使用
IBatis.Net学习笔记(六):Castle.DynamicProxy的使用
namespace GSpring.CastleTest
2、实现类:
IBatis.Net学习笔记(六):Castle.DynamicProxy的使用using System;
IBatis.Net学习笔记(六):Castle.DynamicProxy的使用
using System.Collections.Generic;
IBatis.Net学习笔记(六):Castle.DynamicProxy的使用
using System.Text;
IBatis.Net学习笔记(六):Castle.DynamicProxy的使用
IBatis.Net学习笔记(六):Castle.DynamicProxy的使用
namespace GSpring.CastleTest
这两个都很普通的接口和实现
3、代理类:
IBatis.Net学习笔记(六):Castle.DynamicProxy的使用using System;
IBatis.Net学习笔记(六):Castle.DynamicProxy的使用
using System.Collections;
IBatis.Net学习笔记(六):Castle.DynamicProxy的使用
using System.Reflection;
IBatis.Net学习笔记(六):Castle.DynamicProxy的使用
using Castle.DynamicProxy;
IBatis.Net学习笔记(六):Castle.DynamicProxy的使用
IBatis.Net学习笔记(六):Castle.DynamicProxy的使用
namespace GSpring.CastleTest
这个类首先实现接口IInterceptor,然后就可以在方法Intercept中加入我们自己的逻辑

然后看一下调用的方式:
IBatis.Net学习笔记(六):Castle.DynamicProxy的使用        ProxyGenerator proxyGenerator = new ProxyGenerator();
IBatis.Net学习笔记(六):Castle.DynamicProxy的使用        IInterceptor handler 
= new InterceptorProxy();
);
最后一句调用的地方,实际会首先执行InterceptorProxy类中的Intercept方法。

相关文章:

  • 2021-06-07
  • 2021-09-14
  • 2021-09-15
  • 2021-10-26
  • 2022-01-25
  • 2021-05-19
  • 2021-09-14
  • 2021-12-12
猜你喜欢
  • 2021-05-18
  • 2022-12-23
  • 2021-08-20
  • 2022-12-23
  • 2021-07-27
  • 2022-01-03
相关资源
相似解决方案