Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In order to generate code based on a database table, the template has to somehow know about the database table. This means supplying metadata through a property that refers to the table. Fortunately, CodeSmith includes the SchemaExplorer library, which contains a rich set of types designed specifically for interacting with databases. One of these types, TableSchema, allows the user to pick a table from a database. You can then use the object model in the SchemaExplorer library to retrieve just about any information you need about the table and the database. Here's the Property directive that we need:

Code Block
languagehtml/xml
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the Web service will access." %>

CodeSmith itself doesn't have any special knowledge of the types in the SchemaExplorer library, so we need to tell it to load the assembly containing the library. It's also useful to import the SchemaExplorer namespace to keep the amount of typing we have to do to a minimum:

Code Block
languagehtml/xml
<%@ Assembly Name="SchemaExplorer" %>

...


<%@ Import Namespace="SchemaExplorer" %>

When the user selects a table with SchemaExplorer, the TableSchema object will be populated and returned to CodeSmith. For the most part, this particular template can be filled out just by retrieving the names of the table, the table's owner, and the database from this object. All of those are easily available by navigating around the SchemaExplorer object model:

Code Block
languagevb
<%= SourceTable.Name %>

...


<%= SourceTable.Owner %>

...


<%= SourceTable.Database.Name %>

Substituting those expressions in appropriate places will get you most of the way through writing this particular template. But there's still one task left that requires a bit of coding: building the list of column names for the stored procedure.

...