天天看點

CodeSmith使用體驗和心得!

CodeSmith之初體驗

CodeSmith相關文章:

CodeSmith使用體驗和心得

Struts+Spring+Hibernate練習(完整)

struts+spring+hibernate之間的關系與差别(轉)

史上最簡單的struts+spring+hibernate配置執行個體 修訂版

配置Springframework與hibernate連接配接多資料庫的事務

Spring+Hibernate連接配接Mysql的(Tomcat)配置

JDO,EJB/CMP,Hibernate和Amber比較(翻譯附原文)

最全的C\C++面試題集(C\C++試題和部分答案)

最全的C\C++面試題集(最全的C\C++試題集和答案)(續)

對于代碼生成工具的迷戀好象從使用IBuySpy開發asp.net項目起就開始了。用過QuickCode,也自己寫過Cool Coder,但是卻一直沒有使用曾被推薦為《每個開發人員現在應該下載下傳的十種必備工具 》的CodeSmith,不能說不是遺憾,今天就看看它有什麼特别之處。

相對于其他代碼生成器而言,CodeSmith的最大的特點就是它有自己的程式設計語言,可以自己開發各種模版,特别是它對于基于資料庫的代碼開發,也是吸引我的主要原因,不過今天我們先看個簡單的例子,看看它是如何來生成代碼的。

CodeSmith使用體驗和心得!

程式代碼 <%@ CodeTemplate Language="C#" TargetLanguage="C#"

      Description="Generates a class including a special informational header" %>

<%@ Property Name="NameSpace" Type="String"

      Category="Context"

      Description="The namespace to use for this class" %>

<%@ Property Name="ClassName" Type="String"

      Category="Context"

      Description="The name of the class to generate" %>

<%@ Property Name="DevelopersName" Type="String"

      Category="Context"

      Description="The name to include in the comment header" %>

///

// File: <%=ClassName%>.cs

// Description: Enter summary here after generation.

// ---------------------

// Copyright © <%= DateTime.Now.Year %> Our Client

// ---------------------

// History

//    <%= DateTime.Now.ToShortDateString() %>    <%= DevelopersName%>    original Version

///

using System;

namespace <%=NameSpace %>

{

      /// <summary>

      /// Summary description for <%=ClassName %>.

      /// </summary>

      public class <%=ClassName %>

      {

            public <%=ClassName %>()

            {

                  //

                  // TODO: Add constructor logic here

                  //

            }

      }

}

看着上面的代碼,是不是感覺很熟悉?是的,它的文法很像asp.net的文法,這樣,大家就不需要花費太多時間來學習了。

其生成結果如下:

CodeSmith使用體驗和心得!

程式代碼 /**

// File: Hello.cs

// Description: Enter summary here after generation.

// ---------------------

// Copyright c 2005 Our Client

// ---------------------

// History

//    2005-1-18    Tim Wang    original Version

/**

using System;

namespace ASPCOOL

{

      /** <summary>

      /// Summary description for Hello.

      /// </summary>

      public class Hello

      {

            public Hello()

            {

                  //

                  // TODO: Add constructor logic here

                  //

            }

      }

}

CodeSmith官方介紹:

CodeSmith is a template-based code generator that can produce code for any text-based language. Whether your target language is C#, Visual Basic .NET, T-SQL, Java or even FORTRAN, CodeSmith can help you produce higher-quality, more consistent code in less time than writing code by hand. CodeSmith's familiar ASP.NET-based template syntax means that you can be writing your first templates within minutes of installing the package. The advanced CodeSmith Studio integrated development environment (IDE) helps you create and test new templates in a rapid development setting. You can also join in CodeSmith's active online community to download hundreds of ready-made templates for such common development tasks as building strongly-type collection classes or creating data access layers.

CodeSmith also includes a console version that you can easily integrate into your automated build process, flexible strategies for merging generated code with custom code, the SchemaExplorer API for integration with relational data sources, and the ability to hook up your own custom metadata sources.

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,舉個例子:

CodeSmith使用體驗和心得!

程式代碼 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檔案中。例如:

CodeSmith使用體驗和心得!

程式代碼 <?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運作模闆  

CodeSmith template模版資源:

在對 codeSmith初步了解

以後,對于要還要程式設計才能生産代碼實在有點失望(飛鷹的懶惰程度有此可見了)。幸好,網上有許多現成的模版可供下載下傳,現搜集如下:

CodeSmith Tempalate share 論壇。

Codesmith templates library

DotNet DAL Library

總結:

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

更多 CodeSmith .Net相關文章:

CodeSmith向導——快速上手(翻譯)

《 每個開發人員現在應該下載下傳的十種必備工具 》

.net中的正規表達式使用進階技巧 (一)

.net中的正規表達式使用進階技巧 (二)

Asp.Net 2.0中得益于靜态内容的七個關鍵程式設計技巧

用VS2005開發ASP.NET 2.0資料庫程式(一)

用VS2005開發ASP.NET 2.0資料庫程式(二)

用VS2005開發ASP.NET 2.0資料庫程式(三)

ASP.NET 2.0的資料通路和緩存資料

Struts+Spring+Hibernate練習(完整)

struts+spring+hibernate之間的關系與差别(轉)

CodeSmith

轉載于:https://www.cnblogs.com/xiazhi33/articles/941203.html