CodeSmith基础(三)

CodeSmith基础(三)

2005-12-19 17:49 by 努力学习的小熊, 14513 visits, 收藏编辑
        这里写的东东都是从CodeSmith自带的帮助文档中FAQ里学到的东东
        1.如何在模板中添加注释
        CodeSmith:
        <%-- Comments --%>
        VB.NET:
        <%-- 'Comments --%>
        C#:
        <%-- // Comments --%>
        <%-- /* Comments */ --%>

        2.创建一个可以下拉选择的属性
        首先定义一个枚举类型的变量,然后将属性的类型设置为枚举型
 1  <% @ Property Name = " CollectionType "  Type = " CollectionTypeEnum "  Category = " Collection "  Description = " Type of collection "   %>
 2 
 3  < script runat = " tempate " >
 4  public enum CollectionTypeEnum
 5  {
 6      Vector,
 7      HashTable,
 8      SortedList
 9  }
10  </ script >

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

        4.如何声明一个常量
       
< script runat = " template " >
private const string MY_CONST 
=   " example "
</ script >

        5.如何对模板进行调试
        如果要调试一个模板,首先要在代码模板里进行声明,然后在你想要进行调试的地方用Debugger.Break()语句设置断点即可。
<% @ CodeTemplate Language = " C# "  TargetLanguage = " T-SQL "  Description = " Debugging your template "  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  (TableSchema table  in  SourceDatabase.Tables) 
 3
 4   OutputSubTemplate(table); 
 5}
 
 6 %>
 7 < script runat = " template " >  
 8 private  CodeTemplate _mySubTemplate;
 9
10 [Browsable( false )]
11 public  CodeTemplate MySubTemplate 
12
13   get 
14   
15      if (_mySubTemplate == null
16      
17         CodeTemplateCompiler compiler = new CodeTemplateCompiler(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 (int i = 0; i < compiler.Errors.Count; i++
26            {
27               Response.WriteLine(compiler.Errors[ i].ToString()); 
28            }
 
29         }
 
30      }
 
31      return _mySubTemplate; 
32   }
 
33}

34
35 public   void  OutputSubTemplate(TableSchema table) 
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存储过程
        SubTemplatesExample.cst文件源代码
 1 <% @ CodeTemplate Language = " C# "  TargetLanguage = " T-SQL "
 2     Description = " Generates a update stored procedure. "   %>
 3
 4 <% @ Property Name = " SourceDatabase "  Type = " SchemaExplorer.DatabaseSchema "
 5     Category = " Context "
 6     Description = " Database "   %>
 7
 8 <% @ Assembly Name = " SchemaExplorer "   %>
 9
10 <% @ Import Namespace = " SchemaExplorer "   %>
11
12 <%  
13 foreach  (TableSchema table  in  SourceDatabase.Tables) 
14
15   OutputSubTemplate(table); 
16}
 
17 %>  
18
19 < script runat = " template " >  
20 private  CodeTemplate _mySubTemplate; 
21  
22
23   
24
25 [Browsable( false )]
26 public  CodeTemplate MySubTemplate 
27
28   get 
29   
30      if (_mySubTemplate == null
31      
32         CodeTemplateCompiler compiler = new CodeTemplateCompiler(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 (int i = 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 public   void  OutputSubTemplate(TableSchema table) 
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文件源代码
 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     
13 < script runat = " template " >
14 public   string  GetSqlParameterStatement(ColumnSchema column)
15 {
16    string param = "@" + column.Name + " " + column.NativeType;
17
18    switch (column.DataType)
19    {
20        case DbType.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    return param;
36}

37 </ script >
38
39 -----------------------------------------------------------------
40 --  Date Created:  <%=  DateTime.Now.ToLongDateString()  %>
41 --  Created By:   Generated by CodeSmith
42 -----------------------------------------------------------------
43
44 CREATE PROCEDURE dbo.Update <%=  SourceTable.Name  %>
45      <%   for  ( int  i  =   0 ; i  <  SourceTable.Columns.Count; i ++ %>
46    <%= GetSqlParameterStatement(SourceTable.Columns[i]) %><% if (i < SourceTable.Columns.Count - 1%>,<% } %>
47    <% }
  %>
48 AS
49
50 UPDATE [ <%=  SourceTable.Name  %> ] SET
51      <%   for  ( int  i  =   0 ; i  <  SourceTable.NonPrimaryKeyColumns.Count; i ++ %>
52    [<%= SourceTable.NonPrimaryKeyColumns[i].Name %>= @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><% if (i < SourceTable.NonPrimaryKeyColumns.Count - 1%>,<% } %>
53    <% }
  %>
54 WHERE
55      <%   for  ( int  i  =   0 ; i  <  SourceTable.PrimaryKey.MemberColumns.Count; i ++ %>
56    <% if (i > 0%>AND <% } %>
57    [<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>= @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>
58    <% }
  %>

        8.在加载模板时默认加载的命名空间Namespaces和组件Assemblies
        组件:mscorlib, System, System.Xml, System.Data, System.Drawing, Microsoft.VisualBasic, System.Windows.Forms, CodeSmith.Engine
        命名空间:System, System.Data, System.Diagnostics, System.ComponentModel, Microsoft.VisualBasic, CodeSmith.Engine

        9.使用SchemaExplorer能否确定一个字段(Field)是标识字段(主键,Identity Field)
        在字段的扩展属性集合中包含一个叫“CS_IsIdentity”的属性,如果这个属性的值为true,则表名当前字段为一个标识字段
1  Identity Field  =   <%   foreach (ColumnSchema cs  in  SourceTable.Columns) {  
2         if ( (( bool )cs.ExtendedProperties[ " CS_IsIdentity " ].Value)  ==   true )
3        {
4              Response.Write(cs.Name);
5        }
6  }
7  %>
        CS_Identity_Example.cst文件源代码
 1  <% @ CodeTemplate Language = " C# "  TargetLanguage = " T-SQL "
 2      Description = " Identifies the identity field of a table "   %>
 3 
 4  <% @ Property Name = " SourceTable "  Type = " SchemaExplorer.TableSchema "
 5      Category = " Context "
 6      Description = " Table to target. "   %>
 7 
 8  <% @ Assembly Name = " SchemaExplorer "   %>
 9 
10  <% @ Import Namespace = " SchemaExplorer "   %>
11 
12 
13 
14  Identity Field  =   <%   foreach (ColumnSchema cs  in  SourceTable.Columns) {  
15                           if ( (( bool )cs.ExtendedProperties[ " CS_IsIdentity " ].Value)  ==   true )
16                          {
17                              Response.Write(cs.Name);
18                          }
19                      }
20                    %>

        10.如何确定一个字段的默认值(各人认为翻译成如何知道一个字段有默认值并且默认值是什么)
        在字段的扩展属性集合中包含一个叫“CS_Default”的属性

1  <%
2  foreach (ColumnSchema cs  in  SourceTable.Columns) {  
3         if  (cs.ExtendedProperties[ " CS_Default " !=   null )
4        {
5             Response.WriteLine(cs.ExtendedProperties[ " CS_Default " ].Value);
6        }
7  }
8  %>

        11.如何使用SchemaExplorer得到存储过程的输入输出参数
        使用CodeSmith提供的CommandSchema对象,它包含需要的输入输出参数集合
 1  Input Parameters: 
 2  <% foreach (ParameterSchema ps  in  SourceProcedure.AllInputParameters)
 3  {
 4        Response.Write(ps.Name);
 5        Response.Write( " \n " );
 6  }
 7  %>
 8 
 9 
10  Output Parameters: 
11  <% foreach (ParameterSchema ps  in  SourceProcedure.AllOutputParameters)
12  {
13        Response.Write(ps.Name);
14        Response.Write( " \n " );
15  }
16  %>
        InputOutputParameterExample.cst文件源代码
 1  <% @ CodeTemplate Language = " C# "  TargetLanguage = " T-SQL "
 2      Description = " Generates a update stored procedure. "   %>
 3 
 4  <% @ Property Name = " SourceProcedure "  Type = " SchemaExplorer.CommandSchema "
 5      Category = " Context "
 6      Description = " The stored procedure to examine "   %>
 7 
 8  <% @ Assembly Name = " SchemaExplorer "   %>
 9 
10  <% @ Import Namespace = " SchemaExplorer "   %>
11 
12  Input Parameters: 
13  <% foreach (ParameterSchema ps  in  SourceProcedure.AllInputParameters)
14  {
15      Response.Write(ps.Name);
16      Response.Write( " \n " );
17  }
18  %>
19 
20 
21  Output Parameters: 
22  <% foreach (ParameterSchema ps  in  SourceProcedure.AllOutputParameters)
23  {
24      Response.Write(ps.Name);
25      Response.Write( " \n " );
26  }
27  %>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值