CodeSmith基础(三)
这里写的东东都是从CodeSmith自带的帮助文档中FAQ里学到的东东
1.如何在模板中添加注释
CodeSmith:
<%--Comments --%>
VB.NET:
<%--'Comments --%>
C#:
<%--// Comments --%>
<%--/* Comments */ --%>
2.创建一个可以下拉选择的属性
首先定义一个枚举类型的变量,然后将属性的类型设置为枚举型
3.解决ASP.NET中标签<%重复问题
先将ASP.NET中使用的这个重复标签写成<%%,避免在生成代码时由于是标签重复引起的编译错误或生成错误。
4.如何声明一个常量
5.如何对模板进行调试
如果要调试一个模板,首先要在代码模板里进行声明,然后在你想要进行调试的地方用Debugger.Break()语句设置断点即可。
6.如何将属性设置成选择一个文件夹的路径
7.怎样调用子模板
SubTemplatesExample.cst文件源代码
1.如何在模板中添加注释
CodeSmith:
<%--Comments --%>
VB.NET:
<%--'Comments --%>
C#:
<%--// Comments --%>
<%--/* Comments */ --%>
2.创建一个可以下拉选择的属性
首先定义一个枚举类型的变量,然后将属性的类型设置为枚举型
1<%@PropertyName="CollectionType"Type="CollectionTypeEnum"Category="Collection"Description="Typeofcollection"%>
2
3<scriptrunat="tempate">
4publicenumCollectionTypeEnum
5{
6Vector,
7HashTable,
8SortedList
9}
10</script>
2
3<scriptrunat="tempate">
4publicenumCollectionTypeEnum
5{
6Vector,
7HashTable,
8SortedList
9}
10</script>
3.解决ASP.NET中标签<%重复问题
先将ASP.NET中使用的这个重复标签写成<%%,避免在生成代码时由于是标签重复引起的编译错误或生成错误。
4.如何声明一个常量
<scriptrunat="template">
privateconststringMY_CONST="example";
</script>
privateconststringMY_CONST="example";
</script>
5.如何对模板进行调试
如果要调试一个模板,首先要在代码模板里进行声明,然后在你想要进行调试的地方用Debugger.Break()语句设置断点即可。
<%@CodeTemplateLanguage="C#"TargetLanguage="T-SQL"Description="Debuggingyourtemplate"Debug="true"%>
<%Debugger.Break();%>
<%Debugger.Break();%>
6.如何将属性设置成选择一个文件夹的路径
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor),typeof(System.Drawing.Design.UITypeEditor))]
publicstringOutputDirectory
{
get{return_outputDirectory;}
set{_outputDirectory=value;}
}
publicstringOutputDirectory
{
get{return_outputDirectory;}
set{_outputDirectory=value;}
}
7.怎样调用子模板
1
<%
2
foreach(TableSchematableinSourceDatabase.Tables)
3

{
4
OutputSubTemplate(table);
5
}
6
%>
7
<scriptrunat="template">
8
privateCodeTemplate_mySubTemplate;
9
10
[Browsable(false)]
11
publicCodeTemplateMySubTemplate
12

{
13
get
14

{
15
if(_mySubTemplate==null)
16

{
17
CodeTemplateCompilercompiler=newCodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName+"MySubTemplate.cst");
18
compiler.Compile();
19
if(compiler.Errors.Count==0)
20

{
21
_mySubTemplate=compiler.CreateInstance();
22
}
23
else
24

{
25
for(inti=0;i<compiler.Errors.Count;i++)
26

{
27
Response.WriteLine(compiler.Errors[i].ToString());
28
}
29
}
30
}
31
return_mySubTemplate;
32
}
33
}
34
35
publicvoidOutputSubTemplate(TableSchematable)
36

{
37
MySubTemplate.SetProperty("SourceTable",table);
38
MySubTemplate.SetProperty("IncludeDrop",false);
39
MySubTemplate.SetProperty("InsertPrefix","Insert");
40
MySubTemplate.Render(Response);
41
}
42
</script>
FAQ中给出的例子为生成一个数据库中所有表的更新Update存储过程
<%2
foreach(TableSchematableinSourceDatabase.Tables)3


{4
OutputSubTemplate(table);5
}6
%>7
<scriptrunat="template">8
privateCodeTemplate_mySubTemplate;9

10
[Browsable(false)]11
publicCodeTemplateMySubTemplate12


{13
get14


{15
if(_mySubTemplate==null)16


{17
CodeTemplateCompilercompiler=newCodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName+"MySubTemplate.cst");18
compiler.Compile();19
if(compiler.Errors.Count==0)20


{21
_mySubTemplate=compiler.CreateInstance();22
}23
else24


{25
for(inti=0;i<compiler.Errors.Count;i++)26


{27
Response.WriteLine(compiler.Errors[i].ToString());28
}29
}30
}31
return_mySubTemplate;32
}33
}34

35
publicvoidOutputSubTemplate(TableSchematable)36


{37
MySubTemplate.SetProperty("SourceTable",table);38
MySubTemplate.SetProperty("IncludeDrop",false);39
MySubTemplate.SetProperty("InsertPrefix","Insert");40
MySubTemplate.Render(Response);41
}42
</script>
SubTemplatesExample.cst文件源代码
1
<%@CodeTemplateLanguage="C#"TargetLanguage="T-SQL"
2
Description="Generatesaupdatestoredprocedure."%>
3
4
<%@PropertyName="SourceDatabase"Type="SchemaExplorer.DatabaseSchema"
5
Category="Context"
6
Description="Database"%>
7
8
<%@AssemblyName="SchemaExplorer"%>
9
10
<%@ImportNamespace="SchemaExplorer"%>
11
12
<%
13
foreach(TableSchematableinSourceDatabase.Tables)
14

{
15
OutputSubTemplate(table);
16
}
17
%>
18
19
<scriptrunat="template">
20
privateCodeTemplate_mySubTemplate;
21
22
23
24
25
[Browsable(false)]
26
publicCodeTemplateMySubTemplate
27

{
28
get
29

{
30
if(_mySubTemplate==null)
31

{
32
CodeTemplateCompilercompiler=newCodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName+"MySubTemplate.cst");
33
compiler.Compile();
34
35
if(compiler.Errors.Count==0)
36

{
37
_mySubTemplate=compiler.CreateInstance();
38
}
39
else
40

{
41
for(inti=0;i<compiler.Errors.Count;i++)
42

{
43
Response.WriteLine(compiler.Errors[i].ToString());
44
}
45
}
46
}
47
48
return_mySubTemplate;
49
}
50
}
51
52
publicvoidOutputSubTemplate(TableSchematable)
53

{
54
MySubTemplate.SetProperty("SourceTable",table);
55
MySubTemplate.SetProperty("IncludeDrop",false);
56
MySubTemplate.SetProperty("InsertPrefix","Insert");
57
58
MySubTemplate.Render(Response);
59
}
60
</script>
MySubTemplate.cst文件源代码
<%@CodeTemplateLanguage="C#"TargetLanguage="T-SQL"2
Description="Generatesaupdatestoredprocedure."%>3

4
<%@PropertyName="SourceDatabase"Type="SchemaExplorer.DatabaseSchema"5
Category="Context"6
Description="Database"%>7

8
<%@AssemblyName="SchemaExplorer"%>9

10
<%@ImportNamespace="SchemaExplorer"%>11

12
<%13
foreach(TableSchematableinSourceDatabase.Tables)14


{15
OutputSubTemplate(table);16
}17
%>18

19
<scriptrunat="template">20
privateCodeTemplate_mySubTemplate;21

22

23

24

25
[Browsable(false)]26
publicCodeTemplateMySubTemplate27


{28
get29


{30
if(_mySubTemplate==null)31


{32
CodeTemplateCompilercompiler=newCodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName+"MySubTemplate.cst");33
compiler.Compile();34

35
if(compiler.Errors.Count==0)36


{37
_mySubTemplate=compiler.CreateInstance();38
}39
else40


{41
for(inti=0;i<compiler.Errors.Count;i++)42


{43
Response.WriteLine(compiler.Errors[i].ToString());44
}45
}46
}47

48
return_mySubTemplate;49
}50
}51

52
publicvoidOutputSubTemplate(TableSchematable)53


{54
MySubTemplate.SetProperty("SourceTable",table);55
MySubTemplate.SetProperty("IncludeDrop",false);56
MySubTemplate.SetProperty("InsertPrefix","Insert");57

58
MySubTemplate.Render(Response);59
}60
</script>
1
<%@CodeTemplateLanguage="C#"TargetLanguage="T-SQL"
2
Description="Generatesaupdatestoredprocedure."%>
3
4
<%@PropertyName="SourceTable"Type="SchemaExplorer.TableSchema"
5
Category="Context"
6
Description="Tablethatthestoredproceduresshouldbebasedon."%>
7
8
<%@AssemblyName="SchemaExplorer"%>
9
10
<%@ImportNamespace="SchemaExplorer"%>
11
12
13
<scriptrunat="template">
14
publicstringGetSqlParameterStatement(ColumnSchemacolumn)
15

{
16
stringparam="@"+column.Name+""+column.NativeType;
17
18
switch(column.DataType)
19

{
20
caseDbType.Decimal:
21

{
22
param+="("+column.Precision+","+column.Scale+")";
23
break;
24
}
25
default:
26

{
27
if(column.Size>0)
28

{
29
param+="("+column.Size+")";
30
}
31
break;
32
}
33
}
34
35
returnparam;
36
}
37
</script>
38
39
-----------------------------------------------------------------
40
--DateCreated:<%=Da
<%@CodeTemplateLanguage="C#"TargetLanguage="T-SQL"2
Description="Generatesaupdatestoredprocedure."%>3

4
<%@PropertyName="SourceTable"Type="SchemaExplorer.TableSchema"5
Category="Context"6
Description="Tablethatthestoredproceduresshouldbebasedon."%>7

8
<%@AssemblyName="SchemaExplorer"%>9

10
<%@ImportNamespace="SchemaExplorer"%>11

12

13
<scriptrunat="template">14
publicstringGetSqlParameterStatement(ColumnSchemacolumn)15


{16
stringparam="@"+column.Name+""+column.NativeType;17

18
switch(column.DataType)19


{20
caseDbType.Decimal:21


{22
param+="("+column.Precision+","+column.Scale+")";23
break;24
}25
default:26


{27
if(column.Size>0)28


{29
param+="("+column.Size+")";30
}31
break;32
}33
}34

35
returnparam;36
}37
</script>38

39
-----------------------------------------------------------------40
--DateCreated:<%=Da
本文介绍了使用CodeSmith进行代码生成的一些实用技巧,包括如何在模板中添加注释、创建可选属性、解决标签重复问题等,并提供了详细的代码示例。
521

被折叠的 条评论
为什么被折叠?



