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 " >
4 publicenumCollectionTypeEnum
5 {
6 Vector,
7 HashTable,
8 SortedList
9 }
10 </ script >
2
3 < scriptrunat = " tempate " >
4 publicenumCollectionTypeEnum
5 {
6 Vector,
7 HashTable,
8 SortedList
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))]
public string OutputDirectory
{
get { return _outputDirectory;}
set {_outputDirectory = value;}
}
public string OutputDirectory
{
get { return _outputDirectory;}
set {_outputDirectory = value;}
}
7.怎样调用子模板
1
<%
2 foreach (TableSchematable in SourceDatabase.Tables)
3 {
4OutputSubTemplate(table);
5}
6 %>
7 < scriptrunat = " template " >
8 private CodeTemplate_mySubTemplate;
9
10 [Browsable( false )]
11 public CodeTemplateMySubTemplate
12 {
13get
14{
15if(_mySubTemplate==null)
16{
17CodeTemplateCompilercompiler=newCodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName+"MySubTemplate.cst");
18compiler.Compile();
19if(compiler.Errors.Count==0)
20{
21_mySubTemplate=compiler.CreateInstance();
22}
23else
24{
25for(inti=0;i<compiler.Errors.Count;i++)
26{
27Response.WriteLine(compiler.Errors[i].ToString());
28}
29}
30}
31return_mySubTemplate;
32}
33}
34
35 public void OutputSubTemplate(TableSchematable)
36 {
37MySubTemplate.SetProperty("SourceTable",table);
38MySubTemplate.SetProperty("IncludeDrop",false);
39MySubTemplate.SetProperty("InsertPrefix","Insert");
40MySubTemplate.Render(Response);
41}
42 </ script >
FAQ中给出的例子为生成一个数据库中所有表的更新Update存储过程2 foreach (TableSchematable in SourceDatabase.Tables)
3 {
4OutputSubTemplate(table);
5}
6 %>
7 < scriptrunat = " template " >
8 private CodeTemplate_mySubTemplate;
9
10 [Browsable( false )]
11 public CodeTemplateMySubTemplate
12 {
13get
14{
15if(_mySubTemplate==null)
16{
17CodeTemplateCompilercompiler=newCodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName+"MySubTemplate.cst");
18compiler.Compile();
19if(compiler.Errors.Count==0)
20{
21_mySubTemplate=compiler.CreateInstance();
22}
23else
24{
25for(inti=0;i<compiler.Errors.Count;i++)
26{
27Response.WriteLine(compiler.Errors[i].ToString());
28}
29}
30}
31return_mySubTemplate;
32}
33}
34
35 public void OutputSubTemplate(TableSchematable)
36 {
37MySubTemplate.SetProperty("SourceTable",table);
38MySubTemplate.SetProperty("IncludeDrop",false);
39MySubTemplate.SetProperty("InsertPrefix","Insert");
40MySubTemplate.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 (TableSchematable in SourceDatabase.Tables)
14 {
15OutputSubTemplate(table);
16}
17 %>
18
19 < scriptrunat = " template " >
20 private CodeTemplate_mySubTemplate;
21
22
23
24
25 [Browsable( false )]
26 public CodeTemplateMySubTemplate
27 {
28get
29{
30if(_mySubTemplate==null)
31{
32CodeTemplateCompilercompiler=newCodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName+"MySubTemplate.cst");
33compiler.Compile();
34
35if(compiler.Errors.Count==0)
36{
37_mySubTemplate=compiler.CreateInstance();
38}
39else
40{
41for(inti=0;i<compiler.Errors.Count;i++)
42{
43Response.WriteLine(compiler.Errors[i].ToString());
44}
45}
46}
47
48return_mySubTemplate;
49}
50}
51
52 public void OutputSubTemplate(TableSchematable)
53 {
54MySubTemplate.SetProperty("SourceTable",table);
55MySubTemplate.SetProperty("IncludeDrop",false);
56MySubTemplate.SetProperty("InsertPrefix","Insert");
57
58MySubTemplate.Render(Response);
59}
60 </ script >
MySubTemplate.cst文件源代码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 (TableSchematable in SourceDatabase.Tables)
14 {
15OutputSubTemplate(table);
16}
17 %>
18
19 < scriptrunat = " template " >
20 private CodeTemplate_mySubTemplate;
21
22
23
24
25 [Browsable( false )]
26 public CodeTemplateMySubTemplate
27 {
28get
29{
30if(_mySubTemplate==null)
31{
32CodeTemplateCompilercompiler=newCodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName+"MySubTemplate.cst");
33compiler.Compile();
34
35if(compiler.Errors.Count==0)
36{
37_mySubTemplate=compiler.CreateInstance();
38}
39else
40{
41for(inti=0;i<compiler.Errors.Count;i++)
42{
43Response.WriteLine(compiler.Errors[i].ToString());
44}
45}
46}
47
48return_mySubTemplate;
49}
50}
51
52 public void OutputSubTemplate(TableSchematable)
53 {
54MySubTemplate.SetProperty("SourceTable",table);
55MySubTemplate.SetProperty("IncludeDrop",false);
56MySubTemplate.SetProperty("InsertPrefix","Insert");
57
58MySubTemplate.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 public string GetSqlParameterStatement(ColumnSchemacolumn)
15 {
16stringparam="@"+column.Name+""+column.NativeType;
17
18switch(column.DataType)
19{
20caseDbType.Decimal:
21{
22param+="("+column.Precision+","+column.Scale+")";
23break;
24}
25default:
26{
27if(column.Size>0)
28{
29param+="("+column.Size+")";
30}
31break;
32}
33}
34
35returnparam;
36}
37 </ script >
38
39 -----------------------------------------------------------------
40 -- DateCreated: <%= Da
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 public string GetSqlParameterStatement(ColumnSchemacolumn)
15 {
16stringparam="@"+column.Name+""+column.NativeType;
17
18switch(column.DataType)
19{
20caseDbType.Decimal:
21{
22param+="("+column.Precision+","+column.Scale+")";
23break;
24}
25default:
26{
27if(column.Size>0)
28{
29param+="("+column.Size+")";
30}
31break;
32}
33}
34
35returnparam;
36}
37 </ script >
38
39 -----------------------------------------------------------------
40 -- DateCreated: <%= Da
评论