IBatis.Net学习笔记十一:Castle.DynamicProxy的使用
Castle是另外一个框架,包含了AOP、IOC、ORM等多个方面,其中的Castle.DynamicProxy可以实现动态代理的功能,这个也是很多框架的基础。在IBatis.Net中就是使用了Castle.DynamicProxy来实现数据库连接等动态操作的。同时在NHibernet等其他框架中也使用到了这个技术。
下面我通过一个简单例子来看一下如何在我们的代码中调用Castle.DynamicProxy:
一般情况下要有三个类:
1、接口类:
using System;
using System.Collections.Generic;
using System.Text;
![](/Images/OutliningIndicators/None.gif)
namespace GSpring.CastleTest
{
public interface ITest
{
string GetName(string pre);
}
}
2、实现类:
using System;
using System.Collections.Generic;
using System.Text;
![](/Images/OutliningIndicators/None.gif)
namespace GSpring.CastleTest
{
public class Test : ITest
{
public string GetName(string pre)
{
return pre + ",GSpring";
}
}
}
这两个都很普通的接口和实现
3、代理类:
using System;
using System.Collections;
using System.Reflection;
using Castle.DynamicProxy;
![](/Images/OutliningIndicators/None.gif)
namespace GSpring.CastleTest
{
/// <summary>
/// Summary description for DaoProxy.
/// </summary>
public class InterceptorProxy : IInterceptor
{
public object Intercept(IInvocation invocation, params object[] arguments)
{
Object result = null;
![](/Images/OutliningIndicators/InBlock.gif)
//这里可以进行数据库连接、打日志、异常处理、权限判断等共通操作
result = invocation.Proceed(arguments);
![](/Images/OutliningIndicators/InBlock.gif)
return result;
}
![](/Images/OutliningIndicators/InBlock.gif)
}
}
这个类首先实现接口IInterceptor,然后就可以在方法Intercept中加入我们自己的逻辑
然后看一下调用的方式:
ProxyGenerator proxyGenerator = new ProxyGenerator();
IInterceptor handler = new InterceptorProxy();
Type[] interfaces = { typeof(ITest) };
Test test = new Test();
ITest iTest = (proxyGenerator.CreateProxy(interfaces, handler, test) as ITest);
string result = iTest.GetName("Hello");
最后一句调用的地方,实际会首先执行InterceptorProxy类中的Intercept方法。
下面我通过一个简单例子来看一下如何在我们的代码中调用Castle.DynamicProxy:
一般情况下要有三个类:
1、接口类:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
3、代理类:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
然后看一下调用的方式:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)