使用CodeSmith2.6

來源:互聯網
上載者:User
SCR:http://www.cnblogs.com/elevenWolf/archive/2004/12/01/71391.html
器大家都用過吧,CodeSmith我想大家也不陌生。最近就用CodeSmith產生了自己的實體類。當然你也可以用它來產生HTML頁面以及文檔。

下面我就來說一說我產生實體類的步驟:
一、首先介紹一下CodeSmith 2.6,CodeSmith 2.6安裝後,會有3個exe:
1.       CodeSmith.exe即為CodeSmith Explorer,可視化產生代碼工具,免費,沒有時間限制
2.       CodeSmithConsole.exe 在Command模式下運行,免費,沒有時間限制
3.       CodeSmithStudio.exe編輯模板的工具的工具,可以校正模板文法是否正確。試用30天。
CodeSmith模板支援C#、VB.Net文法,可以調用.net類庫,文法和.net基本上是一樣的。  

二、製作模板
1.在這裡我選擇了C#作為模板的開發語言。
<%@ CodeTemplate Language="C#" TargetLanguage="Text" Description="Template description here." %> 
2.要產生資料庫的實體類,資料連線和表名不可缺少的。在CodeSmith中通過SchemaExplorer.dll來幫你和資料庫打交道。
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
在模板中匯入SchemaExplorer。
然後設定一系列的需要傳入的參數:
<%@ Property Name="DataBase" Type="SchemaExplorer.DatabaseSchema" Category="Context" Description="資料庫連接." %>
<%@ Property Name="TableName" Type="System.String" Default="" Optional="False" Category="" Description="表名" %> 

3.CodeSmith模板指令碼格式:
<script runat="template"> 
       CodeTemplateRule rule=new CodeTemplateRule();
</script>  

或者:
/// <summary> 
    /// 作用:<%= Description %> 
    /// 作者:<%= Author %> 
    /// 日期:<%= DateTime.Now.ToString() %>
///</summary>  

4.我自己寫了一個dll來存放自己的函數 CodeTemplateRule.dll ,其中引用到了SchemaExplorer,舉個例子:

public ColumnSchemaCollection GetColumnCollection(DatabaseSchema dataBase,string tableName) 
         { 
              TableSchemaCollection tables = new TableSchemaCollection(dataBase.Tables); 
              ColumnSchemaCollection columns=null; 
              for(int i=0;i<tables.Count;i++) 
              { 
                   if(tables[i].Name.ToUpper()==tableName.ToUpper()) 
                   { 
                        TableSchema ts=tables[i]; 
                        columns=new ColumnSchemaCollection(ts.Columns); 
                   } 
              } 
              return columns; 
         } 

這段代碼的含義就是取資料庫中某張表所有列的集合。

見Demo檔案:
模板檔案:entity.cst
自己寫的.net程式集:CodeTemplateRule.cs
產生後的代碼效果:AccountBookEntity.cs  

三、運行
1.       用CodeSmith.exe運行模板,CodeSmith會彈出對話方塊來你來填寫你的參數。
2.       用CodeSmithConsole.exe運行模板,參數可以放在xml檔案中。例如:
<?xml version="1.0" encoding="utf-8" ?>

<codeSmith> 

       <propertySets>

              <propertySet>

                     <property name="SampleStringProperty1">string111111111</property>

                     <property name="SampleBooleanProperty1">false</property>         

              </propertySet>         

       </propertySets>

</codeSmith>

然後用命令執行:

3.       在CodeSmithStudio.exe運行模板  

總結:

代碼產生器給我們編程工作帶來了很大的便利,不需要做很多重複性的工作。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.