Code Generators are nothing new. I played around with Codesmith back before they started charging $200 for it and have looked into some of the codegen tools on sourceforge but was generally unimpressed. Most lacked enough documentation to really customise anything.
I even tried some of the high-end tools like Tier Developer which costs $1k and provided everything but the kitchen sink. You can download it for the trial, but it requires a steep learning curve to customise and you actually get a salesman call you up on the phone.
If you’ve never used a code generator, it’s basically an application that spits out code based on collections you pass it. Most of the time, it’s a database structure. You design your database, point your code generator at it, and select which tables you want to use. The template will create some output files for you based on what you select. For example, I selected five tables and used a code generator to create my CRUD stored proc files. I select a directory to output to and the code generators put the files there. I can then run these files against a database or include them in a Visual Studio database project. Since this is a one-shot generation, I can freely modify these files after they’ve been created.
I first looked at MyGeneration a few months ago. Like most of the cool new tools, I thought about all the cool things I could do with it– then I moved on to something else.
Today I started automating some of my Model classes using it. So I got to really use it in anger. Here are the things I really like about it:
-
- It’s free.
- There is a huge template library
- Tempates can be written in VBScript– so no new languages to learn.
Every time I look through a template library for a code generator, I get some really ambitious code. Everyone wants to generate all of your stored procs, data access layers, business objects, and test cases. I usually just want something simple like business logic layer. With this tool, it’s easy to write your own.
In a few hours, I wrote code to generate all of my interfaces and model objects. Since it was vbs, it was just like writing a classic asp page. I just looped through all the tables and created the beginning of my business objects:
<% '******************************Class File************************************* %> #region headerblock // Filename : <%=tableName%>.cs // Change history // date version: author : description of change // <%=Now()%> <%=vbtab%>0.1 <%=vbtab%><%=input.Item("txtDeveloperName")%> <%=vbtab%>Object Created #endregion using System; using System.Data; using System.Collections; namespace <%= TrimSpaces(namespace) %> { public class <%=tableName %> : I<%=tableName %> { public <%= tableName %>() { } #region Internals <% For Each objColumn in objTable.Columns %> private <%=objColumn.LanguageType%> <%=InternalVariableName(objColumn,true)%>;<%Next%> #endregion #region Properties <% For Each objColumn in objTable.Columns %> public <%=objColumn.LanguageType%> <%=InternalVariableName(objColumn,false)%> { get { return <%=InternalVariableName(objColumn,true)%>; } set { <%=InternalVariableName(objColumn,true)%> = value; } } <%Next%> #endregion } } <% call SaveFile(tableName,input.item("txtPath")) %>
One of the most tedious aspects of creating a model project is generating this code. Sure, I need to modify this further and I will probably normalise the database further–but this gives me a decent start and saves a few hours. I can also use this to generate the very basic unit tests (ie. can I create the object? Do all of the properties match?, etc).
It’s definitely a tool to check out. It’s a lot more fun to create your own template (or modify an existing one) than it is to use what’s given to you.
You can find MyGeneration at : http://www.mygenerationsoftware.com
I was tempted to title this post “Talking ’bout MyGeneration” but I fought back the urge.