This article will demonstrate how to add and customize "ColumnGrid" extended property in CodeSmith. This property
allows user to add extended information to columns in the table without using Sql Server
"extended information".
Here is a screenshot of what it looks like when you click on the "ColumnGrid" property in CodeSmith.
Background
I started using CodeSmith about 2 months ago. I used to write my own code generation tools, but at this
point decided to start using CodeSmith because of its popularity with developer community. I was especially
excited about being able to add "extended properties" to codesmith to customize its functionality. However,
it didn't take me long to find out that 2-3 sample custom properties included in CodeSmith was all the
documentation and support I was going to get. After some contemplation if I should go back to writing
my own tools, I decided to get my hands dirty and work through this obstacle. The result is "ColumnGrid"
extended property. As you can see from screen shot above the control will pull the name of the table
from "SourceTable" property and provide an editable row for each column. Each row will consist of
extended properties that you can use for code generation. I was able to successfully use this control in
many of the templates I created for my XWebSiteTemplate, .NET framework for developing 3-tier web applications.
Implementation
You can download all the source code for "ColumnGrid" extended property from here:
Column Grid Source Code
The project is called "XWebSiteTemplate_CustomProperties" and it consists of 5 files:
|
Column.cs
|
Contains column extended properties.
|
|
ColumnGridProperty.cs
|
Main property class.
Links to Editor (for UI) and Serializer (for saving and loading).
|
|
ColumnGridPropertyEditor.cs
|
Provides UI when user clicks on the Property id CodeSmith.
|
|
ColumnGridPropertySerializer.cs
|
Contains code for saving and loading property information from XML data store.
Used when you click on 'Save Values Set' or 'Load Values Set' in CodeSmith.
|
|
ColumnsGridForm.cs
|
Form containing grid view with editable column information
Instantiated and displayed by ColumnGridPropertyEditor.cs
|
After you rebuild the project, you need to copy resulting DLL "XWebSiteTemplate.CustomProperties.dll"
to "[Code Smith Dir]\v4.0\AddIns". Then you can start using "ColumnGrid" property directly from
CodeSmith.
Using ColumnGrid within CodeSmith
Here is an example of using ColumnGrid property to display table columns that have
extended property "AllowSort" checked:
1. <%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="True" %>
2.
3. <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Database" %>
4. <%@ Property Name="ColumnGrid" Type="ColumnGridProperty" Category="Options" %>
5.
6. <%@ Assembly Name="SchemaExplorer" %>
7. <%@ Import Namespace="SchemaExplorer" %>
8.
9. <%@ Assembly Name="XWebSiteTemplate.CustomProperties" %>
10. <%@ Import Namespace="XWebSiteTemplate.CustomProperties" %>
11.
12. <%
13. TableSchema tableSchema=(TableSchema)this.GetProperty("SourceTable");
14. ColumnSchema columnSchema;
15.
16. foreach(XWebSiteTemplate.CustomProperties.Column columnExtInfo in this.ColumnGrid.Columns)
17. {
18.
19. columnSchema=tableSchema.Columns[columnExtInfo.Name];
20.
21. if (columnExtInfo.AllowSort)
22. Response.WriteLine(columnSchema.Name + ", " + columnSchema.DataType);
23.
24. }
25. %>
You can view the file without those pesky line numbers here.
Let's go through this example.
Lines 3 and 4 declare properties used in this template. "SourceTable" property allows user to select a
database table used for code generation. "Column Grid" property of type "ColumnGridProperty" will
retreive columns from "SourceTable" and display a form with a list of columns and extended properties
which can be modified by the user.
Lines 6-10 make available SchemaExplorer library as well as XWebSiteTemplate.CustomProperties library which
contains "ColumnGridProperty".
Lines 13-14 declare tableSchema and columnSchema variables used for retreiving ColumnSchemas
matching columns in "ColumnGrid" property.
Lines 16-24 loop through the columns saved in ColumnGrid, find associated ColumnSchemas and
display column names and types only for columns with "AllowSort" checked.
Here is a screenshot of this template in action:
To get to this point I executed "sample.cst" template in CodeSmith studio, selected "Address" table for
"SourceTable" property then clicked on "..." button that appears when you click on value box next to
"ColumnGrid" property.
Let's say I only want "City" and "State" to be "sortable". To do that I unchecked all check-boxes in "AllowSort"
column except for "City" and State".
When I click OK in "ColumnGrid" form and then "Generate" in CodeSmith I get the following results:
City, AnsiString
State, AnsiString
As expected - only "sortable" columns are displayed.
Customizing ColumnGrid - adding fields to extended info
You may have noticed in the previous section that there are 5 columns in the ColumnGrid.
Name of the "table column" is read-only and four other columns contain editable check-box fields.
I used fields "AllowSearch", "AllowSort", "IsRequired" and "DisplayInSearchResults" specifically for
generating code within XWebSiteTemplate framework. However you can easily add your own editable fields.
Let's say you would like to add a "summary" textbox, which will be used to generate summary XML tag for
each object property (associated with the column). To do that:
- Open "XWebSiteTemplate_CustomProperties" project in VS 2005
- Modify Column.cs: Add "_summary" private member and property "Summary"
- Modify ColumnGridProperty.cs: In method 'GenColumnFromSchema' Set default value for "Summary". This step can be skipped.
- Modify ColumnGridSerializer.cs: In methods 'WritePropertyXml' and 'ReadPropertyXml' add a line to save and load "Summary" from XML
- Open form GolumnGridForm.cs: Add column 'Summary' and bind it to DataPropertyName 'Summary'
- Save all files and re-build
- Copy updated XWebSiteTemplate_CustomProperties.dll to 'CodeSmith' AddIns directory
- Now you should be able to edit the text box in 'ColumnGrid' property and then access it using 'XWebSiteTemplate.CustomProperties.Column.Summary
Actually the easiest way to add a new editable field is just to search the project for every occurance
of already existing property such as "AllowSort". Then add code for the new property next to the
existing one.
References & Downloads
|
Download "ColumnGrid" AddIn and sample code
|
Includes:
- XWebSiteTemplate_CustomProperties library that contains "ColumnGrid" property
- All source code for "XWebSiteTemplate_CustomProperties"
- Sample.cst file used in "Using ColumnGrid within CodeSmith" section of the article
|
|
Code Smith
|
Flexible and versatile tool for code generation.
|
|
Download CodeSmith templates for XWebSiteTemplate
|
Templates to generate object classes and web pages for 3-tier web applications using XWebSiteTemplate.
Templates ItemSearch.cst, EditItemPageASPX.cs, SearchItemsPageASPX use ColumnGrid property.
|
Technorati Profile
|