Greenator
Greenator is just another code generator.
Greenator V0.1 published at http://workspaces.gotdotnet.com/greenator!
First steps in using Greenator are descriped in a article.
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”.
VS .NET has a built in interface for integrating Code Generation called custom tool. Two Custom Tools (also called Generators) come with VS.NET, MSDataSetGenerator for strongly-typed DataSets, and MSDiscoCodeGenerator for WebService proxy classes.
Once you have set the property custom tool the generator starts and generates a subordinate project item with the generated source. Every time you change and save your base definition file, the source file is generated again.
I like the transparent way how the generation works. In fact, at first sight it is a miracle how the strongly typed dataset is generated from a Schema.
It is possible to write your own custom tools. All you need is to implement one function:
public override byte[] GenerateCode(string file, string contents)
{
string code = "<<generated code>>";
return System.Text.Encoding.ASCII.GetBytes(code);
}
On a second sight, this approach limits the scope for active code generation. Code generation should be under your control, you should e.g. set the templates to be used for the code generation. It is not possible to set a further argument inside the properties of the definition file.
Some code generators that implement custom tool cheat; they add the reference of the template inside the model definition. That is not orthogonal.
Furthermore it is not possible to generate two code files out of one model.
Greenator is written as Visual Studio Add-In and for this reason Greenator is not limited by the design of custom tool. But Greenator supplements custom tools behaviour, it watches all templates and models and generates the code file or even files transparent on the fly every time it must.
I used consciously code generation one year ago. Nearly every day I was asked to implement and publish another contact form at our company’s homepage: Address fields, email address, custom validations…Actual a common task, but none of these forms looked like another, all of them asked for at least one different field. It was a stupid and boring. The approach was simple too: The form definition has been reduced to a xml based model. I used XSLT successful to transform these models to .ascx user controls.
The model files have been maintained by some power users. They did not know much about XML, but they have been able to alter existing model files to their needs. My function was reduced to validate the assigned model, to run the transformation and to publish the output to a web server. The time was reduced from about one hour per form to few minutes. As a side effect oversights diasppeared.
Did you ever have a "Deja vu" in your daily programming live? Do you have too the feeling of repeating again and again similar lines of code? Do you used to copy and paste your source code?
No? You must be lucky. Perhaps you are using a language like ruby, which is dynamic enough to let be do what you want without leaving the language.
Yes? But you worked hard, you have powerfull libraries. You have refactored your code again and again, but this feeling never moved away?
Now its time for Code Generation!