Back in February, as a follow-up to my exploration of Aspect Oriented Programming (part one, two, three), I took a look at Composite Oriented Programming. And then I kinda forgot about it… I remember speaking with Anders Norås at an NNUG Oslo event shortly after I had written my post, and he said he was planning to have a deeper look at COP himself; well, the other day he wrote a post about it over on his blog.
Dynapose
Back when I wrote my post I actually spent a fair amount of time implementing a C# COP framework based on my previous AOP work. So my initial post wasn’t just a theoretical “what if” kind of thing – it was actually compilable, executable code – I called it the Dynapose framework (a play on “Dynamic Composition” – clever, no? :p). The plan was to put it up on Codeplex, but I never made it that far and it just ended up collecting dust on the shelf instead. Well no more - if you want to play about with it then go to the end of this post for the download link.
Since Last Time…
In my original sample from back in February, I was using attributes as a way of configuring the composites. Later I also added a fluent interface style configuration API, which allowed composites to be dynamically built at runtime:
IDivision division = Composer.Compose<IDivision>()
.AddMixin<ICalculationDataAspect, CalculationDataAspect>()
.AddMixin<ICalculationLogicAspect, DivisionLogicAspect>()
.AddConcern<DivisionValidationConcern>()
.Instantiate();
You can also use a mix of attributes and dynamic configuration, to for example specify a default composite and then create variations of it. The fluent API will also let you give it a delegate to use as a factory method for mixins etc:
.AddMixin<ICalculationLogicAspect>(() => new DivisionLogicAspect())
My plan with this was to allow my framework to be integrated with IOC containers, which would have been totally awesome.
Known Bugs & Limitations
Well, its been a long while since I worked with this code so I’m not 100% sure on the state of things. What I do remember is that I was having a bit of trouble with generics, so that might not work as expected.
Try it Out!
If you want to play around with this yourself, then you can download the source code here. It includes a simple demo application, and the source code is fairly well documented and tested. I’ve no idea if I’ll pursue this thing any further, so take it for what it is: a fun little proof of concept project that you probably don’t want to get anywhere near your production code :p
If you do check it out, please let me know what you think, and what you made happen with it!