[Note: This is by NO means an exhaustive discussion of code gen, and only illustrates the most miniscule example of what you can do and how it can be done.]
One of the topics I really have an interest in is code generation ('code gen' for short). Some call it useless, while others, such as myself, find it compelling. I don't know how I would live without it, since writing redundant code is one of those things I have much apathy for. Why write code that can be generated with the click of a button?
For those not familiar with this concept, I will give you a simplified example. Let's say you have a SQL Server database that has a table with the following schema:
Now, you usually have to perform the usual CRUD operations on such a table, with various variations, but for the most part, we can generally assume you want to INSERT new records into this table, SELECT all the data from the table, UPDATE records, and DELETE records. You get the idea.
We can *easily* generate a set of 4 stored procedures (sprocs) to do these things. It is ridiculously easy in fact. There are sprocs extant after you install SQL Server that will allow you to obtain metadata about tables and their fields, their relationships, etc. Once we have this data, we can generate our sprocs. Example code for generating the SELECT sproc:
// let's assume the name of the table is in this string variable, "tableName" // and all of the fields of the table are in this array variable, "fieldNameArray" // (we obtained that info by querying the database for the metadata)
string selectAllSproc = "CREATE PROCEDURE " + tableName + "_SelectAll AS SELECT ";
for(int i = 0; i < fieldNameArray.Length; i++) { selectAllSproc += fieldNameArray[i]; selectAllSproc += (i == (fieldNameArray.Length - 1)) ? " " : ", "; }
selectAllSproc += "FROM " + tableName + " GO";
When this is done, our string selectAllSproc has the value:
CREATE PROCEDURE User_SelectAll AS SELECT UserID, Username, Password, FirstName, LastName, DateCreated, Status FROM User GO
So, if we were to print that to a text file (probably would be formatted better), we would have a script we could run, thus creating the sproc in our database (obviously there is a little more to it, but really that is the essence).
Pretty nice. Imagine your database has 45 tables. Do you really want to hand-craft all of that code? You should be shaking your head "NO" right now. So, not only does this save us time, but it improves consistency, because we know that if this runs right once, it will do the same for all tables (Again, I am generalizing, I don't want to write a book on the subject!).
So, in our schema, we have this field "Status" right? Maybe I only want that column to contain 2 different values, namely "Active" or "Inactive". Well first off we need a check constraint attached to that table indicating that those are the only valid values. Again, we can query the database and find this out so that programmatically, we can access that info. Well, this is perfect for generating an enumeration like this:
public enum Status { Active, Inactive }
This can then be used in our code when we want to give someone the option of choosing a value for that field, thereby helping strongly-type the data being passed around (read: maintain the integrity of data, thereby reducing the possibility of errors).
Although simplified, I hope this example shows just what kind of possibilities there are for using code generation in your development process. It can truly be a boon to productivity and overall code quality if done well.
Remember Me
a@href@title, b, blockquote@cite, i, strike, u
Disclaimer The opinions expressed herein are Jason Bunting's personal opinions, as they are presently constituted, and are subject to change and do not represent his employer's view in any way; feel free to disagree with him. Jason Bunting is not a doctor, and no advice/information presented on this website is intended to diagnose, treat or cure any disease. When you have health questions, always seek help from a qualified health practitioner or naturopathic physician. This information is provided as-is, without warranty, and is solely the opinion of Jason Bunting, whose opinions may differ from mainstream medical opinion.