CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jay Kimble -- The Dev Theologian

Philosophizing about the .Net religion

One CodeSmith Template Shall Rule Them All...

I just finished building the ultimate CodeSmith Template for myself.  Let me give a little back drop to this.

I have a Data Access Layer Template that I simply point the template to all my select, list, and CRUD (Create, Update, and Delete) Stored Procs and it builds the following classes for me:
1) SQL data communications - the only place in my code that communicates with the database server
2) Data Bus Object - a class that represents a single row from the Stored Proc
3) Data Collection - a collection containing the previously generated bus object
4) Controller - a class that is used by the GUI to communicate with the other layers; it exposes all the actions that can take place on the data (Create, Update and Delete)
[I have a couple additional classes I will be adding, but for now this is what I have]
I have come to a place where I need more actions than the standard select, list, create, update, and delete.  Sometimes I have a special update that only updates a single field, or where I need to maybe get all the IDs that are ready for the next step of a workflow (trying to be very generic in my explanation here).  Anyway, what I ran into was that I want more actions than I'm currently accounting for.
To solve this problem, I created a new CodeSmith template to create My Data Access Layer Template; in short a template that code generates other templates. 
Here's how to do it.  First of all start with a working template.  What made things easy for me was I created a constant that represents the start and end script tags (<% and %>) in a string like this:
<script runat="Template">
 Const __b as String = "<%"
 Const __n as String = "%>"
</script>
Or in C#
<script runat="Template">
 const string __b = "<%";
 const string __n = "%>";
</script>
Next, I changed all the occurrences of the "%>" to "%%>"; now you can do a search and replace on "<%" and change it to "<%=__b%>", and finally you can do a global search and replace on "%%>" changing it to "<%=__n%>."  Now you have a template that will generate another template.

Simply edit like you would any other template.  You'll have to remember that the stuff between the "<%=__b%>" and "<%=__n%>" tags will be script in the new template and that the editor won't highlight this.

Pretty nifty if I must say so myself.



Check out Devlicio.us!

This Blog

Syndication

News

CodeBetter.Com Home
Current Threat level
Terror Alert Level