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 Generator 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 | ||
---|---|---|
| ||
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the Web service will access." %> |
CodeSmith CodeSmith Generator 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:
...
When the user selects a table with SchemaExplorer, the TableSchema object will be populated and returned to CodeSmith Generator. 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 name from this object. All of those are easily available by navigating around the SchemaExplorer object model:
Code Block | ||
---|---|---|
| ||
<%= SourceTable.Name %> <%= SourceTable.Owner %> <%= SourceTable.Database.Name %> |
...