一、第一個模板
創建好一個模板後第一步要指明這是一個C#語言的模板。
<%@ CodeTemplate Language="C#" TargetLanguage="C#"
Description="Generates a class including a special informational header" %>
Description="Generates a class including a special informational header" %>
第二步,我們要指明模板在生成代碼時的屬性,即生成代碼需要的輸入值變量。
<%@ Property Name="NameSpace" Type="String"
Category="Context"
Description="The namespace to use for this class" %>
Category="Context"
Description="The namespace to use for this class" %>
如上邊所示,在進行代碼生成時,在CodeSmith Explorer中選擇模板後生成代碼的窗口中,變量的名稱爲NameSpace,類型是String,類別是Context,當用戶選中這個屬性時對於屬性的描述Description。
我們可以按照C#語言的語法去使用定義的變量,例如:
例如下面這個例子模板使用了上面介紹的知識。Test.cst
///////////////////////////////////////////////////////////////////////////////////////
// File: <%=ClassName%>.cs
// File: <%=ClassName%>.cs
<%@ CodeTemplate Language="C#" TargetLanguage="C#"
Description="Generates a class including a special informational header" %>
<%@ Property Name="NameSpace" Type="String"
Category="Context"
Description="The namespace to use for this class" %>
<%@ Property Name="ClassName" Type="String"
Category="Context"
Description="The name of the class to generate" %>
<%@ Property Name="DevelopersName" Type="String"
Category="Context"
Description="The name to include in the comment header" %>
Description="Generates a class including a special informational header" %>
<%@ Property Name="NameSpace" Type="String"
Category="Context"
Description="The namespace to use for this class" %>
<%@ Property Name="ClassName" Type="String"
Category="Context"
Description="The name of the class to generate" %>
<%@ Property Name="DevelopersName" Type="String"
Category="Context"
Description="The name to include in the comment header" %>
///////////////////////////////////////////////////////////////////////////////////////
// File: <%=ClassName%>.cs
// Description: Enter summary here after generation.
// ---------------------
// Copyright ? <%= DateTime.Now.Year %> Our Client
// ---------------------
// History
// <%= DateTime.Now.ToShortDateString() %> <%= DevelopersName%> Original Version
///////////////////////////////////////////////////////////////////////////////////////
using System;
namespace <%=NameSpace %>
{
/// <summary>
/// Summary description for <%=ClassName %>.
/// </summary>
public class <%=ClassName %>
{
public <%=ClassName %>()
{
//
// TODO: Add constructor logic here
//
}
}
}
// File: <%=ClassName%>.cs
// Description: Enter summary here after generation.
// ---------------------
// Copyright ? <%= DateTime.Now.Year %> Our Client
// ---------------------
// History
// <%= DateTime.Now.ToShortDateString() %> <%= DevelopersName%> Original Version
///////////////////////////////////////////////////////////////////////////////////////
using System;
namespace <%=NameSpace %>
{
/// <summary>
/// Summary description for <%=ClassName %>.
/// </summary>
public class <%=ClassName %>
{
public <%=ClassName %>()
{
//
// TODO: Add constructor logic here
//
}
}
}
然後我們在CodeSmith Explorer中雙擊這個模板就會看到相應的屬性界面,這裏的屬性均是我們在前邊定義的屬性。
按下Generate按鈕生成,即可實現一個簡單的類代碼的生成。
1///////////////////////////////////////////////////////////////////////////////////////
2// File: MyClass.cs
3// Description: Enter summary here after generation.
4// ---------------------
5// Copyright ? 2003 Our Client http://www.livebaby.cn
6// ---------------------
7// History
8// 12/2/2003 Mr. Smith Original Version
9///////////////////////////////////////////////////////////////////////////////////////
10
11using System;
12
13namespace MyNameSpace
14{
15 /// <summary>
16 /// Summary description for MyClass.
17 /// </summary>
18 public class MyClass
19 {
20 public MyClass()
21 {
22 //
23 // TODO: Add constructor logic here
24 //
25 }
26 }
27}
2// File: MyClass.cs
3// Description: Enter summary here after generation.
4// ---------------------
5// Copyright ? 2003 Our Client http://www.livebaby.cn
6// ---------------------
7// History
8// 12/2/2003 Mr. Smith Original Version
9///////////////////////////////////////////////////////////////////////////////////////
10
11using System;
12
13namespace MyNameSpace
14{
15 /// <summary>
16 /// Summary description for MyClass.
17 /// </summary>
18 public class MyClass
19 {
20 public MyClass()
21 {
22 //
23 // TODO: Add constructor logic here
24 //
25 }
26 }
27}
生成後的代碼即可放入Visual Studio .NET中使用,我們使用CodeSmith的目的就是爲了快速高效的開發。
二、數據庫模板
下面這個例子將介紹CodeSmith與數據庫進行交互生成相應的存儲過程,本例使用的數據庫爲SQL Server 2000。
在與數據庫進行交互時,我們使用到了一個CodeSmith自帶的組件SchemaExplorer,利用這個組件我們可以訪問數據庫的數據表、存儲過程、視圖等,並可以得到相應的數據類型、標識列、列的(字段)名稱等信息。
下面這個例子是教我們如何生成一個存儲過程,雖然網上有很多一樣的例子,但是我是從CodeSmith中的英文幫助中自己翻譯出來的:)
使用的是SQL Server 2000自帶的Northwind數據庫,生成一個關於Orders訂單表的更新存儲過程。
第一步還是指明模板使用的語言和生成的目標語言。
第二步就是我們要加載使用訪問數據庫的組件SchemaExplorer,並聲明其使用的命名空間。
因爲是針對表去生成存儲過程,則首先要定義一個存儲表名稱使用的變量,然後指明這個變量類型爲數據庫中的表,這樣我們可以通過這個數據表類型的變量得到相應的表的信息。
如果想訪問視圖的話,則將變量類型Type中的SchemaExplorer.TableSchema修改爲SchemaExplorer.ViewSchema即可。
得到表名的方法
下面利用循環語句遍歷表的各個列,拼出存儲過程需要傳遞的參數。
調用的GetSqlParameterStatement方法是用來生成參數的字符串,例如生成“@CustomerID nchar(5)”,後邊緊跟的if判斷是用來生成參數之間相隔使用的逗號的。
生成參數字符串的方法,參數爲SchemaExplorer.ColumnSchema列類型
下面來生成需要更新的字段,更新時僅能更新非主鍵字段的值,在SchemaExplorer中支持這種區別,使用SourceTable.NonPrimaryKeyColumns即可得到非主鍵字段的集合。
以下爲整體的代碼結構
在與數據庫進行交互時,我們使用到了一個CodeSmith自帶的組件SchemaExplorer,利用這個組件我們可以訪問數據庫的數據表、存儲過程、視圖等,並可以得到相應的數據類型、標識列、列的(字段)名稱等信息。
下面這個例子是教我們如何生成一個存儲過程,雖然網上有很多一樣的例子,但是我是從CodeSmith中的英文幫助中自己翻譯出來的:)
使用的是SQL Server 2000自帶的Northwind數據庫,生成一個關於Orders訂單表的更新存儲過程。
第一步還是指明模板使用的語言和生成的目標語言。
<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL" Description="Generates a update stored procedure." %>
第二步就是我們要加載使用訪問數據庫的組件SchemaExplorer,並聲明其使用的命名空間。
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
因爲是針對表去生成存儲過程,則首先要定義一個存儲表名稱使用的變量,然後指明這個變量類型爲數據庫中的表,這樣我們可以通過這個數據表類型的變量得到相應的表的信息。
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the stored procedures should be based on." %>
如果想訪問視圖的話,則將變量類型Type中的SchemaExplorer.TableSchema修改爲SchemaExplorer.ViewSchema即可。
得到表名的方法
CREATE PROCEDURE dbo.Update<%= SourceTable.Name %>
下面利用循環語句遍歷表的各個列,拼出存儲過程需要傳遞的參數。
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
<%= GetSqlParameterStatement(SourceTable.Columns[i]) %><% if (i < SourceTable.Columns.Count - 1) { %>,<% } %>
<% } %>
<%= GetSqlParameterStatement(SourceTable.Columns[i]) %><% if (i < SourceTable.Columns.Count - 1) { %>,<% } %>
<% } %>
調用的GetSqlParameterStatement方法是用來生成參數的字符串,例如生成“@CustomerID nchar(5)”,後邊緊跟的if判斷是用來生成參數之間相隔使用的逗號的。
生成參數字符串的方法,參數爲SchemaExplorer.ColumnSchema列類型
1 <script runat="template">
2 public string GetSqlParameterStatement(ColumnSchema column)
3 {
4 string param = "@" + column.Name + " " + column.NativeType;
5
6 switch (column.DataType)
7 {
8 case DbType.Decimal:
9 {
10 param += "(" + column.Precision + ", " + column.Scale + ")";
11 break;
12 }
13 default:
14 {
15 if (column.Size > 0)
16 {
17 param += "(" + column.Size + ")";
18 }
19 break;
20 }
21 }
22
23 return param;
24 }
25 </script>
2 public string GetSqlParameterStatement(ColumnSchema column)
3 {
4 string param = "@" + column.Name + " " + column.NativeType;
5
6 switch (column.DataType)
7 {
8 case DbType.Decimal:
9 {
10 param += "(" + column.Precision + ", " + column.Scale + ")";
11 break;
12 }
13 default:
14 {
15 if (column.Size > 0)
16 {
17 param += "(" + column.Size + ")";
18 }
19 break;
20 }
21 }
22
23 return param;
24 }
25 </script>
下面來生成需要更新的字段,更新時僅能更新非主鍵字段的值,在SchemaExplorer中支持這種區別,使用SourceTable.NonPrimaryKeyColumns即可得到非主鍵字段的集合。
1UPDATE [<%= SourceTable.Name %>] SET
2 <% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
3 [<%= SourceTable.NonPrimaryKeyColumns[i].Name %>] = @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><% if (i < SourceTable.NonPrimaryKeyColumns.Count - 1) { %>,<% } %>
4 <% } %>
2 <% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
3 [<%= SourceTable.NonPrimaryKeyColumns[i].Name %>] = @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><% if (i < SourceTable.NonPrimaryKeyColumns.Count - 1) { %>,<% } %>
4 <% } %>
然後再使用SourceTable.PrimaryKey.MemberColumns得到數據表中的主鍵集合,生成更新條件
1 WHERE
2 <% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
3 <% if (i > 0) { %>AND <% } %>
4 [<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>
5 <% } %>
2 <% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
3 <% if (i > 0) { %>AND <% } %>
4 [<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>
5 <% } %>
以下爲整體的代碼結構
1 <%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"
2 Description="Generates a update stored procedure." %>
3
4 <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"
5 Category="Context"
6 Description="Table that the stored procedures should be based on." %>
7
8 <%@ Assembly Name="SchemaExplorer" %>
9
10 <%@ Import Namespace="SchemaExplorer" %>
11
12 <script runat="template">
13 public string GetSqlParameterStatement(ColumnSchema column)
14 {
15 string param = "@" + column.Name + " " + column.NativeType;
16
17 switch (column.DataType)
18 {
19 case DbType.Decimal:
20 {
21 param += "(" + column.Precision + ", " + column.Scale + ")";
22 break;
23 }
24 default:
25 {
26 if (column.Size > 0)
27 {
28 param += "(" + column.Size + ")";
29 }
30 break;
31 }
32 }
33
34 return param;
35 }
36 </script>
37
38 -----------------------------------------------------------------
39 -- Date Created: <%= DateTime.Now.ToLongDateString() %>
40 -- Created By: Generated by CodeSmith
41 -----------------------------------------------------------------
42
43 CREATE PROCEDURE dbo.Update<%= SourceTable.Name %>
44 <% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
45 <%= GetSqlParameterStatement(SourceTable.Columns[i]) %><% if (i < SourceTable.Columns.Count - 1) { %>,<% } %>
46 <% } %>
47 AS
48
49 UPDATE [<%= SourceTable.Name %>] SET
50 <% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
51 [<%= SourceTable.NonPrimaryKeyColumns[i].Name %>] = @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><% if (i < SourceTable.NonPrimaryKeyColumns.Count - 1) { %>,<% } %>
52 <% } %>
53 WHERE
54 <% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
55 <% if (i > 0) { %>AND <% } %>
56 [<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>
57 <% } %>
58
2 Description="Generates a update stored procedure." %>
3
4 <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"
5 Category="Context"
6 Description="Table that the stored procedures should be based on." %>
7
8 <%@ Assembly Name="SchemaExplorer" %>
9
10 <%@ Import Namespace="SchemaExplorer" %>
11
12 <script runat="template">
13 public string GetSqlParameterStatement(ColumnSchema column)
14 {
15 string param = "@" + column.Name + " " + column.NativeType;
16
17 switch (column.DataType)
18 {
19 case DbType.Decimal:
20 {
21 param += "(" + column.Precision + ", " + column.Scale + ")";
22 break;
23 }
24 default:
25 {
26 if (column.Size > 0)
27 {
28 param += "(" + column.Size + ")";
29 }
30 break;
31 }
32 }
33
34 return param;
35 }
36 </script>
37
38 -----------------------------------------------------------------
39 -- Date Created: <%= DateTime.Now.ToLongDateString() %>
40 -- Created By: Generated by CodeSmith
41 -----------------------------------------------------------------
42
43 CREATE PROCEDURE dbo.Update<%= SourceTable.Name %>
44 <% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
45 <%= GetSqlParameterStatement(SourceTable.Columns[i]) %><% if (i < SourceTable.Columns.Count - 1) { %>,<% } %>
46 <% } %>
47 AS
48
49 UPDATE [<%= SourceTable.Name %>] SET
50 <% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
51 [<%= SourceTable.NonPrimaryKeyColumns[i].Name %>] = @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><% if (i < SourceTable.NonPrimaryKeyColumns.Count - 1) { %>,<% } %>
52 <% } %>
53 WHERE
54 <% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
55 <% if (i > 0) { %>AND <% } %>
56 [<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>
57 <% } %>
58