Monday, April 12, 2004

CodeSmith revisited #1

I should point out that I originally looked at the CommandWrapperClass template (ships with CodeSmith).  While I'm sure the author of the template put a lot of thought into it, it appears to be a bit more than I need for my test project.  I do think I'll end up using some of the code from the CommandWrapperClass template, but I'm not absolutely sure yet.

So, I need to decide if I want all of my wrapper methods to be in a single, huge class or if I want them in table-related classes.  The latter seems like the right thing to do, but now I have to figure out how to do it. :-)

Writing CodeSmith templates is almost identical to writing ASP.NET.  You have the <% %> tags and the <%@ directives.  You can have code-behind files to do some heavy lifting as well as script in the template itself.

Here's what I have so far:

<%@ CodeTemplate Language="C#" TargetLanguage="Text" Description="Creates DAL" %>
<%@ Property Name="Namespace" Type="System.String" Description="The namespace this class belongs to."%>
<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" Category="Context" Description="Database" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="SchemaExplorer" %>

namespace <%=Namespace%> {

}

Because templates can be written in C#, VB.NET and JScript, the first directive specifies which language I'll be using.  You can create properties that are settable using the properties window by using the Property directive.  I've created two properties; one for the namespace I want to use and the second is for specifying the stored procedure I want to wrap.

I then reference and import the SchemaExplorer namespace.  This lets me interrogate the database schema and retrieve the database objects so I can use them when creating the template.  This means the template will be able to generate all of the SqlParameters by looping through the database objects.

Just for the heck of it, here's some test code to dump all of my stored procedure names along with their parameters:

 <%
  foreach(CommandSchema cs in SourceDatabase.Commands) {
   Response.WriteLine(cs.Name);
   foreach(ParameterSchema ps in cs.Parameters) {
    Response.WriteLine("\t" + ps.Name);
   }
  }
 %>

 


Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?