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.

June 16, 2005

JetBrains MPS: Meta Programming System

The folks over at JetBrains, makers of the excellent IntelliJ IDE, have released their latest project MPS or Meta Programming System. MPS appears to be a Language Workbench. The concept seems extremely interesting, and could potentially be a huge change with how systems are developed.

MPS can be downloaded through the EAP program JetBrains offers. MPS appears to have a dependency on the latest IntelliJ IDEA code named Irida, which too is available through JetBrains' EAP. The MPS is itself a plug-in for Irida IDE.

June 12, 2005

Languages , languages and more languages.

Earlier this year I started working with .NET, C# specifically. Since then I have also started to jump on the Ruby bandwagon. Most recently I've gone back in time and started re-learning C++. It's enjoyable spending time and getting into each language. I think it is valuable to have an understanding of what else is out there. Without a doubt I can see how expanding my knowledge with these languages will benefit me in general. I find that opening my mind to different concepts helps me with me think of things like: "How can I do something similar to this in Java?", or "What is the equivalent syntax for this in C#."

Next steps Ruby on Rails... or maybe I'll have to break out my old SmallTalk books.