CodeSmith Code Generation

I had always had the need in the back of my head to write a Code Generation tool, but could never get around to it. I had found an early open source LLBLGen. I downloaded the code, and started to futz with it before I was forced to work on something else.

About two months ago, I stumbled on CodeSmith. When I saw CodeSmith, I immediately liked that it just wrote code, and left everything else out. LLBLGen is great I am sure, but I don't have the time, patience, and experience to use a complete “O/R Mapper” for my developer businesss of one person. So I got all excited, then forgot all about it once again.

Two weeks ago, I was forced to write some stored procedures that would be perfect to use CodeSmith for, so I decided to try it out. Sure enough, the trial had expired. I uninstalled/reinstalled and it worked. 🙂 Anyway, I read through the Tutorials, looked through the many sample files, and messed around. It didn't take more than a few hours to have something I could use.

Recently, I needed to start a new project that required some Business Objects. No way was I going to do that by hand anymore. I pulled up CodeSmith, and within 2 – 3 hours, created my template. It worked like a charm.

First you decide what input variables you need. The main one is to select what table(s) you want to generate your code from:

Which will take your template

And turn it into code

I am definately going to poney up the $99 for it. That will allow me to use CodeSmith's UI to write the template files. Without it you are forced to use notepad. Yuck!

By the way, the code generated above is not from tblCustomers, just in case any of you were wondering why there would be a “Contact” field there. And to put it all out on the table, the code is generated from a table I didn't create. Point is, don't flame me!

So I have no created two CodeSmith template files. One to generate the insert, update, select, search, and delete stored procedures for a given table, and the other to generate the business object class and collection. Next I will create one for my .ASPX files and codebehind. And if I don't feel like doing it myself, I can augment an existing template file someone has already done and uploaded to the Template CodeSmith Forum.


4 responses to “CodeSmith Code Generation”

  1. Hey Scott,

    Codesmith is truly brilliant. Not only is there a great bunch of community templates, but it’s a doddle to make your own as you say. I’ve been generating the datalayer and biz objects for a while, and just started doing the web admin screens in the last couple of projects.

    The best thing is that each time you start a new project, you can start with your old templates, spend a couple of hours tweaking them to include those extra feature you found missing during your last project. Your templates can continually improve. I use it in conjunction with Paul Wilson’s O/R Mapper. I’ve set things up so it’s pretty easy to add a couple of attributes to the database half way through the project, click regen, and Whabam! The attributes are now instantly added throughout the data layer (mapping file for O/R Mapper), biz objects, and hopefully the next time I get to improve the templates, throughout the UI.

  2. Same story here. A few months back I took the time to build templates for our data and factory layers and also generate the stored procedures using the style and practices we prefer here at work. They even generate the csproj files so I can just include the project in my solution.

    It turns what is a few day project (menial copy and paste), not to mention somewhat error prone, into a simple 5-10 minute process.

    At some point I want to build the unit tests for these too.

  3. hey guys i m very new to code smith.can you guys tell me how can i create a template doing some common tasks like insert update and delete

Leave a Reply

Your email address will not be published. Required fields are marked *