Code Generation with MyGeneration

I had the chance to grab and have a short look at MyGeneration for a few hours over the weekend and I should say I'm quite impressed with the features that it has. [more]

In one of my previous companies we developed our own custom code generator to fit the company's standard process/way of doing things (or more accurately our boss' way of wanting to do things). Being tied to the company's own way of doing things it had a number of constraints such as relying on naming conventions in database tables, support for other databases and only supported generation of C# for the data access layer. Not to mention it's proprietary. The reason I mentioned this is that I think it is safe for me to say that I knew how hard it can be come up with a decent and flexible code generator which supports templates and doesn't require a recompile to even just change the layout of the output. The code generators I've mentioned earlier involved parsing the template and doing simple to complicated string replacements but when I saw MyGeneration, it's awesome.

Unlike custom code generators like the ones I've worked before (and other commercial generators) that is a little more close to one-click code generation though, MyGeneration is more of a text/file (could be code or not) generator which enables you to generate your output(s) given information of your database's meta/structure (tables, columns, indexes, primary keys, foreign keys, views, stored procedures etc) and controlled via templates. The templates can be written in C#, VB.NET, vbscript or jscript and it is in these templates that you say loop inside you table collections and generate your create/insert, retrieve/select, update, delete (CRUD) stored procedures. Did I mention that there are already a good number of templates for generating stored procedures and data access layers for for some known OR/M frameworks? And if they are not enough like and you would want more control over the generated text/code and you happen to know the languages above then go ahead and create your own templates.

You should really go over to the MyGeneration home and familiarize with it, download the stuff. There is also this one link to a video (in youtube and Google video) to get you started. Mind you the resolution of the video is very bad but it still got me through so it's really trivial.

Moving forward, there are already those available for generating CRUD stored procedures which although also includes generation of stored proc by PrimaryKey I needed more such as stored procedures for every index in the tables I selected. I was a bit hesitant since I don't really have the luxury of time but I modify a template anyway and after a few minutes (yes minutes) I was able to come up with one that fits my needs. Yes that's how easy it is and considering that I did it in vbscript (since I modified an existing vbscript template) rather than C# which I'm most familiar with. Anyways, for those interested here's the content of the modified template.

UPDATE 6/22/2008 : as promised uploaded *.vbgen containing the template (not just the actual content). Just drop it inside Templates/Microsoft SQL Server folder and you'd now see it in your Template browser.

Modified dOOdads template for stored procedures with select by indexes follows: RDG_SQL_StoredProcs.vbgen (18.75 kb)

Ohh did I mention that MyGeneration is open source (BSD license) so create as many copies and modify if you have to.

If you already have a code generation of you're own doing then I would still suggest looking into MyGeneration and see it's features and you're really in for a lot of learning. You could either move features of your CG to it and help the community maintain it or get some ideas to your own and credit MyGeneration for it.

If you have the budget then you might want to look into CodeSmith. It's text/code generator too, is commercial but there are templates (such as .netTiers) which aside from generating the code also generates the Visual Studio projects/solutions along with the data access layer. It is likely to give you the same control over generated output as with MyGeneration but I haven't really looked more into this since I'm not looking into a
commercial product as of the moment but will be sure to keep you posted
if I do. As of the moment CodeSmith has schema providers (allows you to look into and retrieve database meta information of MS SQL, Oracle and MySQL as of the current version) but MyGeneration myMeta API supports more providers and has more templates for non .NET code. You have the source code too so if you need to support a custom database/store that you have it is possible and likely relatively easy to create you're own meta provider for MyGeneration too.

Finally, for some people (including a part of me) who are more comfortable with having
much control over their code rather than generated you might hesistate but if you'll be the
one calling the shots on how it would be generated, how much different
could it be? Literally you could save hundreds of hours.

A more detailed comparison might be in place soon but have to get back to work 🙂 Hope you'd get a lot from MyGeneration and this post.