Motivation III: Using XSLT as code generator
I like XSLT. I use it on several occasions; mostly in asp.net context or sharepoint portal server. I am quit familiar with the XSLT syntax.
Why start with code generation? It is no "hen or egg" question. The code you want to generate has been written several times by you. .Net has powerful libraries; you complemented them with your own. You refactored your code. But you couldn't avoid writing boring code again and again. You see the repeating pattern and starts to extract the metadata.
All times I did this; I felt it naturally to write down this metadata in form of XML. No problem to build up hierarchies and transform the XML so it is a real abstraction of your domain. I invent my own tags and I am every time enthusiastic about the result.
As I mentioned in a post yesterday, my first generator generated ascx User Contols. Because I knew XSLT, it was the self-evident idea to use XSLT to transform XML to a HTML-like syntax.
Some weeks ago I had to wrap a set of web services with an object model. Writing the second class in this project I’ve caught the pattern and developed the abstraction as XML. Now I was looking for a code generator that targets C# and searched the web. First hit has been CodeSmith, I installed it and I was really disappointed. I recognized the well known ASP.NET like way, but I was shocked to see that CodeSmith force me to give up my XML representation and to transform it to a .NET based property set. ( to Eric: I studied “PurchaseOrderXML.cst”, that is not what I dream of, sorry). I can imagine requirements where CodeSmith will be the tool of my choice; if have to extract the Meta data out of a database or other sources.
I remembered the XP wisdom “Do the Simplest Thing that Could Possibly Work". I know XSLT, it works. I know how to implement my generator using IE and notepad. That is simple. It doesn’t handicaps me in representing the metadata in its elemental form. Sole constraint: XSLT is different from C# or VB, so you will have a longer learning curve.
It would be easier (not simpler) to have the generator integrated in my IDE: Visual Studio. That is why I started Greenator. Greenator has yet three different generators built in: XSLT, “Simple” and the possibility to configure an external generator like CodeSmith. In a later post I will show you my favourite, “Simple”.