5

我正在实现一个设计,其中我的层将位于客户端和服务器之间,无论我从服务器获得什么对象,我都会将它包装在一个透明代理中并提供给客户端,这样我就可以跟踪对象中的变化,所以当保存回来时,我只会发送更改的信息。

我看了一下城堡动态代理,linfu,虽然他们可以生成代理类型,但是他们不能把现有的对象包装起来。

想知道是否可以使用这些框架,或者是否有任何其他框架可以实现这一点......

4

2 回答 2

7

我们使用无状态实体,并且由于 ASP.NET GridView 的行为,我需要创建一个仅包装现有对象的代理。

我创建了一个拦截器,它以这种方式保存目标实例:

public class ForwardingInterceptor : IInterceptor
{
    private object target;

    private Type type;

    public ForwardingInterceptor(Type type, object target)
    {
        this.target = target;
    }

    public void Intercept(IInvocation invocation)
    {
        invocation.ReturnValue = invocation.Method.Invoke(this.target, invocation.Arguments);
    }       
}

然后你可以简单地创建包装代理:

this.proxyGenerator.CreateClassProxy(type, new ForwardingInterceptor(type, target));
于 2010-05-16T12:27:20.317 回答
4

Castle Dynamic Proxy 3.x 或更高版本可以做到这一点,尽管您必须记住它只能拦截虚拟方法,因此它不是一个完美的抽象。

于 2009-12-03T13:20:10.803 回答