June 17, 2005

Dynamic Proxy missing from C#

There are many things about the C# language I really like and think Java would benefit from having. On the flip side of that several Java features are also sorely missed in C#. One in particular is dynamic proxy. The java implementation of dynamic proxy is java.lang.reflect.Proxy which is an extremely powerful feature. For those unfamiliar with dynamic proxy take a look at the JavaDoc to gain a deeper understanding of it.

For those who aren't interested in reading the JavaDoc here is a quick overview. Basically a dynamic proxy allows you to generate an instance of an Interface(s) at runtime without the need of a real implementation. So for example we have an Interface Foo
public interface Foo {
String hello();
}

We can create an instance of Foo without having to have a class defined which actually implements Foo. In Java we can do this like so:
ClassLoader classLoader = Foo.class.getClassLoader();
Class[] interfaces = new Class[] { Foo.class }; // list of interfaces to support
InvocationHandler invocationHandler = ... // custom impl of InvocationHandler
Foo instance = (Foo) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);

Now the actual power of this is handled in the implementation of the InvocationHandler. Here is a simple implementation of the InvocationHandler which traps method calls to the method hello().
public class FooInvocationHandler implements InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args) {
if(method.getName().equals("hello")) {
System.out.println("The hello method was called!");
}
return "world";
}
}

While this is a very trivial example it does show how easy it is to create a proxy and how potentially powerful they are. Adding AOP type of functionality to method calls is simple to do, such as Security checks and the overused logging example.

Well back to my point. When working on the .NET port of PicoContainer I needed the ability to utilize a dynamic proxy for the ImplementationHidingComponentAdapter. This ComponentAdapter hides the implementation from the container. I figured I would easily be able to convert this logic to the C# port. To my surprise C# does not provide an out of the box simple implementation of dynamic proxy. They do provide the Emit API which will allow you to create your own proxy behavior for it. But creating a sufficient implementation of dynamic proxy from the Emit API is not a trivial task. Luckily the open source community has created an implementation of dynamic proxy for .NET called DynamicProxy.

Best to follow the documentation on the website itself or the related article on The Code Project. It's a good implementation and thoroughly tested. PicoContainer.NET utilizes it, as well as NHibernate. One caveat however is that at the moment it does not work with Mono, hopefully that will be fixed.

Mono error:
** ERROR **: file class.c: line 2820 (mono_ldtoken): should not be reached

It would be great if Microsoft would add dynamic proxy support to the language but until then DynamicProxy seems like an nice tool.

2 comments:

mmrriad said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.