请大家耐心看完所有的基础文章,前两篇网上发表的比较多,是CodeSmith英文帮助文档的第一篇,我后面写的基础是将其他所有的英文帮助全部翻译出来了,全部为本人手写翻译,希望对大家有所帮助
一、第一个模板
创建好一个模板后第一步要指明这是一个C#语言的模板。
生成后的代码即可放入Visual Studio .NET中使用,我们使用CodeSmith的目的就是为了快速高效的开发。
一、第一个模板
创建好一个模板后第一步要指明这是一个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
11 using System;
12
13 namespace 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
11 using System;
12
13 namespace 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即可得到非主键字段的集合。
1
UPDATE
[
<%= 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