IBatis.Net学习笔记(六):Castle.DynamicProxy的使用
2011-04-06 19:40
393 查看
Castle是另外一个框架,包含了AOP、IOC、ORM等多个方面,其中的Castle.DynamicProxy可以实现动态代理的功能,这个也是很多框架的基础。在IBatis.Net中就是使用了Castle.DynamicProxy来实现数据库连接等动态操作的。同时在NHibernet等其他框架中也使用到了这个技术。
下面我通过一个简单例子来看一下如何在我们的代码中调用Castle.DynamicProxy:
一般情况下要有三个类:
1、接口类:
using System;
using System.Collections.Generic;
using System.Text;
namespace GSpring.CastleTest
2、实现类:
using System;
using System.Collections.Generic;
using System.Text;
namespace GSpring.CastleTest
这两个都很普通的接口和实现
3、代理类:
using System;
using System.Collections;
using System.Reflection;
using Castle.DynamicProxy;
namespace GSpring.CastleTest
这个类首先实现接口IInterceptor,然后就可以在方法Intercept中加入我们自己的逻辑
然后看一下调用的方式:
ProxyGenerator proxyGenerator = new ProxyGenerator();
IInterceptor handler = new InterceptorProxy();
Test test = new Test();
ITest iTest = (proxyGenerator.CreateProxy(interfaces, handler, test) as ITest);
string result = iTest.GetName("Hello");最后一句调用的地方,实际会首先执行InterceptorProxy类中的Intercept方法。
下面我通过一个简单例子来看一下如何在我们的代码中调用Castle.DynamicProxy:
一般情况下要有三个类:
1、接口类:
using System;
using System.Collections.Generic;
using System.Text;
namespace GSpring.CastleTest
2、实现类:
using System;
using System.Collections.Generic;
using System.Text;
namespace GSpring.CastleTest
这两个都很普通的接口和实现
3、代理类:
using System;
using System.Collections;
using System.Reflection;
using Castle.DynamicProxy;
namespace GSpring.CastleTest
这个类首先实现接口IInterceptor,然后就可以在方法Intercept中加入我们自己的逻辑
然后看一下调用的方式:
ProxyGenerator proxyGenerator = new ProxyGenerator();
IInterceptor handler = new InterceptorProxy();
Test test = new Test();
ITest iTest = (proxyGenerator.CreateProxy(interfaces, handler, test) as ITest);
string result = iTest.GetName("Hello");最后一句调用的地方,实际会首先执行InterceptorProxy类中的Intercept方法。
相关文章推荐
- IBatis.Net学习笔记(六):Castle.DynamicProxy的使用
- Castle.DynamicProxy的使用
- IBatis.Net学习笔记十一:Castle.DynamicProxy的使用 (转)
- [ibatis]IBatis.Net学习笔记十一:Castle.DynamicProxy的使用
- AOP:PostSharp和Castle DynamicProxy
- Castle.DynamicProxy实现WCF接口服务端模拟
- Java事务(6)——使用动态代理(Dynamic Proxy)
- Castle DynamicProxy creation出现COMException(0x800703fa)错误的解决方案
- Castle 系列:Castle DynamicProxy动态生成透明代理类型详解
- 解决Castle.DynamicProxy的LockBlockExpression在.NET Framework 4.0以上版本歧义问题
- Castle Dynamic Proxy - 2.2
- Castle.DynamicProxy Part 1: ClassProxy
- 基于Autofac, Castle.DynamicProxy的动态WCF解决方案(原创)
- Castle DynamicProxy
- 【框架学习与探究之AOP--Castle DynamicProxy】
- castle.dynamicProxy学习笔记
- Dynamic CRM 2013学习笔记(三十)Linq使用报错 A proxy type with the name account has been defined by another assembly
- 未能加载文件或程序集“Castle.DynamicProxy
- 框架学习与探究之AOP--Castle DynamicProxy
- 使用 Castal DynamicProxy 简化 Silverlight 数据绑定