CodeSmith基础(三)

CodeSmith基础(三)

这里写的东东都是从CodeSmith自带的帮助文档中FAQ里学到的东东
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 >

3.解决ASP.NET中标签<%重复问题
先将ASP.NET中使用的这个重复标签写成<%%,避免在生成代码时由于是标签重复引起的编译错误或生成错误。

4.如何声明一个常量
< scriptrunat = " template " >
privateconststringMY_CONST
= " example " ;
</ script >

5.如何对模板进行调试
如果要调试一个模板,首先要在代码模板里进行声明,然后在你想要进行调试的地方用Debugger.Break()语句设置断点即可。
<% @CodeTemplateLanguage = " C# " TargetLanguage = " T-SQL " Description = " Debuggingyourtemplate " Debug = " true " %>

<% Debugger.Break(); %>

6.如何将属性设置成选择一个文件夹的路径
[Editor( typeof (System.Windows.Forms.Design.FolderNameEditor), typeof (System.Drawing.Design.UITypeEditor))]

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存储过程
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文件源代码
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值