COP, C# 4.0 and doing open source stuff
Quite a few .NET based Composite Oriented Programming spikes have surfaced in the wake of my humble COP spike last week. I was by no means the first to implement this concept on .NET and most of the other spikes were written long before I posted mine. As far as I know Fredrik Kalseth was first to the flag when he wrote about it back in February and he made his code available when he revisited the topic last week. Fredrik ended up in the experimental field of COP after having explored AOP by rolling his own AOP framework. Writing an AOP framework is a fun way to learn lots of things about low-level .NET stuff. I have witnessed all of the challenges with implementing Castle’s Dynamic Proxy, and I would recommend that using this rather than building your own to anyone who gets serious about building a COP framework. Henry Luk, who was the second to do COP on .NET, used Castle Dynamic Proxy and his code is very similar to what my stuff looked like before I added support for references within the composites.
Yves Goleven wrote his COP POC (look anagrams!) with .NET’s own RealProxy and like me he keeps a registry of the “traits” to support dispatching to the pieces of a composite.
The last to join the party was Magnus Mårtensson. Magnus built his “framework” on top of the Unity container and he too used the RealProxy for his proxying needs.
From what I can understand, Magnus is quite determined to go forward with writing a full fledged COP framework. Magnus works for Jayway’s sister company Dotway, and Jayway is of course where the whole Qi4j thing started so Magnus is likely to get the corporate backing needed to take on such a challenge.
There has been some speculation on whether I will get more serious about Qi#, and even if I kind of feel the urge, I won’t do it now for a couple reasons.
I already have my Quaere project which I haven’t been able to do any serious work on for a long time - my apologies to Michael Hunger for bringing that project to an abrupt stop. This was partly because of personal decisions and me not being able to use the stuff I was building in my daily work. I used bits and pieces of Quaere for forthcoming talk at JavaZone and I really want to get that show rolling again soon. I just need to find a way to make me able to be more committed than I am today.
The other reason is more interesting, even though this is more or less pure speculation. On PDC this year Anders Hejlsberg will do a breakout session on the future of C#. The last time he did this he unveiled LINQ and I suspect we have something related to COP coming.
C# 4.0 will have support for dynamic dispatch. The behavior will be similar to Visual Basic’s late binding feature and one of the key motivations for adding it is to improve interoperability with types written in dynamic languages running on the Dynamic Language Runtime. A cool side effect of having dynamic dispatch support in C# is that it will make reflective method invocations much easier to do. Rather than having to go down the tiresome road of getting a MethodInfo from a type and then invoking that method on an instance, we should be able to surround a block with the suggested dynamic keyword and do this:
object myComposite = compositeFactory.Create();
As with Visual Basic’s late binding, the method invocation would of course be bound to an object instance, and hence it would not be as dynamic as Boo’s IQuackFu feature or Ruby’s method_missing. Still, having this kind of dynamic dispatch support would be a step in the right direction for getting intrinsic Composite Oriented Programming support in the language.
While the dynamic keyword isn’t set in stone, dynamic dispatch is more or less a confirmed feature in C# 4.0, but there is another thing I suspect my namesake will unveil in Los Angles this October that is even more interesting…
One of the ingenious things that make LINQ work is the expression trees that are constructed at runtime. These can be inspected, altered and transformed just like the abstract syntax trees used by compilers, they can even be compiled into IL code by invoking the tree’s compile() method. When you think about it, LINQ’s expression trees is a convenient way to build IL code at runtime. This can be extended to also cater for runtime construction and compilation of other structures at runtime, and in a sense this exactly what we’re aiming at with composite oriented programming. Keeping in mind that many of the features we’ve seen in the two latest incarnations of C# are adopted from dynamic languages, I don’t think it is too far fetched to speculate that some form of dynamic object construction will be part of C#.
In combination with dynamic dispatch, such a feature would cover a most of the usage scenarios for COP. There might be some shortcomings with regard to more advanced features such as side effects, but then again C#’s event support covers partially for this.
If dynamic construction becomes part of C#, I’m not going to invest a lot of effort into building a COP framework for .NET. This is the second reason why I won’t start developing such a framework now. I might be wrong in my speculations, and if so could I would be happy to contribute to such a project as long as I’ve got my challenges with being committed to Quaere get sorted. We'll know for sure in October.