views:

291

answers:

2

I am implementing a design where my layer would sit between client and server, and whatever objects i get from server, i would wrap it in a transparent proxy and give to the client, that way i can keep a track of what changed in the object, so when saving it back, i would only send changed information.

I looked at castle dynamic proxy, linfu, although they can generate a proxy type, but they cant take existing objects and wrap them instead.

Wondering if its possible to do with these frameworks, or if there any other frameworks that enable this...

+3  A: 

Castle can wrap existing objects if you're exposing them via interfaces. It can't and won't wrap classes, and for a very good reason.

Krzysztof Koźmic
+1  A: 

We use stateless entities, and due to a behaviour of ASP.NET GridView I needed to create a proxy which would only wrap existing object.

I created an interceptor which keeps a target instance this way:

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);
    }       
}

Then you can simply create the wrapper proxy:

this.proxyGenerator.CreateClassProxy(type, new ForwardingInterceptor(type, target));
jirkamat