codesmith学习总结
生活随笔
收集整理的這篇文章主要介紹了
codesmith学习总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
code smith 使用介紹
利用CodeSmith為SQL Server CE生成項目代碼
?
摘要:CodeSmith是很多.NET開發人員至愛的開發輔助工具,它能夠使開發人員從大量枯燥無味的重復編碼中解脫,集中精力解決實際業務問題和技術問題。本文將介紹如何將CodeSmith用于Windows Mobile項目,以SQL Server Compact Edition數據庫作為代碼生成的依據生成項目代碼。
什么是代碼生成器?
代碼產生器是產生式編程(Generative Programming)在實際應用中的一種產物。它以系統建模為基礎,通過抽象各種系統間的共性與特性,利用代碼模板和組件重用技術,自動產生滿足客戶需求的軟件產品。從而降低成本,減少軟件推向市場的時間,并且保證更好的產品質量。最終取得規模經濟和范圍經濟的優點。2004年我還在大學讀書的時候曾開發過一個基于模板引擎的代碼生成器——RapidTier,當時這個作品還獲得了廣東省高校杯軟件設計大賽的二等獎。在.NET的世界里,目前最有名的代碼生成器當屬CodeSmith和MyGeneration。這兩款代碼生成器都是基于模板引擎的,使用起來方便靈活,而且模板資源非常豐富。
CodeSmith 簡介
CodeSmith 是一個基于模板的代碼生成器,你可以利用它來生成任何文本語言的代碼。CodeSmith 生成的代碼可以通過模板的屬性來定制。CodeSmith 模板的語法跟 ASP.NET 非常相似,你可以在模板中使用 C# 或 VB.NET 控制代碼的生成。通常,開發人員利用CodeSmith根據SQL Server或Oracle等大型數據庫生成各種項目代碼(如:存儲過程、數據訪問層、業務邏輯層、表現層等)。今天將向各位介紹如何利用 CodeSmith為移動數據庫SQL Server Compact Edition生成Windows Mobile項目的代碼。
配置SchemaProvider
CodeSmith通過SchemaProvider來支持各種數據庫。在CodeSmith Professional 4.1.2安裝后,默認只支持SQL Server、Oracle、Access、MySQL等數據庫類型。如果需要支持其他類型的數據庫,可以到網上搜索相關數據庫的 SchemaProvider實現,或者自己手動編寫代碼實現ISchemaProvider接口。
最近,我從網上找到了SQL Server Compact Edition(3.1)的SchemaProvider實現,經過代碼調整和調試,現在跟大家分享一下。可以從這里下載到這個SchemaProvider相關的DLL,下載后將其解壓,并將 SchemaExplorer.SqlCeSchemaProvider.dll和System.Data.SqlServerCe.dll文件復制到 CodeSmith的SchemaProvider目錄下(C:/Program Files/CodeSmith/v4.1/SchemaProviders)。
配置數據源
啟動CodeSmith,點擊左邊停靠的Schema Explorer窗口上方點擊“Manage Data Sources”工具欄按鈕,打開Data Source Manager窗口。
Data Source Manager窗口打開后,點擊Add按鈕添加新的數據源。在Data Source窗口中,數據源名稱(Name)輸入“Northwind sqlce”,提供程序類型(Provider Type)選擇SqlCeSchemaProvider,連接語句輸入“data source=c:/Northwind.sdf”,假設你已經將SQL Server Compact Edition自帶的Northwind.sdf數據庫復制到C盤根目錄了。
你可以通過點擊Test按鈕測試一下是否正常連接,然后點擊OK保存數據源,回到Data Source Manager窗口,現在看到多了一個叫“Northwind sqlce”的數據源了。
“Northwind sqlce”數據源已經添加成功,點擊Close按鈕關閉Data Source Manager窗口。回到Schema Explorer窗口,展開“Northwind sqlce”數據源,瀏覽各個表的字段和屬性。
生成代碼
在CodeSmith右邊停靠的Template Explorer窗口打開自帶的模板“CodeSmith 4.1 Samples/ActiveSnippets/CSharp/TableProperties.cst”。這個模板可以從一個數據庫的表的所有字段生成對應的實體類的所有屬性(Property)定義代碼。
模板被打開后,在CodeSmith右邊停靠的Properties窗口選擇SourceTable屬性,點擊旁邊的“...”按鈕瀏覽并選擇一個Northwind.sdf數據庫的表。這里我們選擇Categories表,并點擊Select按鈕確定。
現在可以按F5生成代碼了,生成的結果如下所示:
private int _categoryID;?
public int CategoryID
{
? ? get { return _categoryID; }
? ? set { _categoryID = value; }
}?
private string _categoryName;?
public string CategoryName
{
? ? get { return _categoryName; }
? ? set { _categoryName = value; }
}?
private string _description;?
public string Description
{
? ? get { return _description; }
? ? set { _description = value; }
}?
private System.Byte[] _picture;?
public System.Byte[] Picture
{
? ? get { return _picture; }
? ? set { _picture = value; }
}
你還可以選擇其他的表或其他模板生成代碼試一試,體驗一下CodeSmith的強大之處。
總結
通過上面的介紹和示例演示,相信大家都認同CodeSmith確實很好很強大。當然,這里只是演示了一個很簡單的例子,你可以根據項目的實際需要自己編寫模板,按自己的方式去生成項目代碼。編寫模板最快捷的方法就是基于現有比較類似的模板進行修改。CodeSmith之所以能夠用于SQL Server Compact Edition數據庫的代碼生成,除了前面提到的它通過SchemaProvider支持各種類型的數據庫,還有一點很重要的就是SQL Server Compact Edition支持桌面平臺,如果是SQL Server Mobile就沒有辦法做到這一點了。
相關下載:SqlCeSchemaProvider.rar
http://upto.cnblogs.com/?
========code Smith 使用學習
?CodeSmith在執行模版時通過調用一些API來完成的,主要經過了以下這幾步的操作:? ? ? ? ?編譯一個模版
? ? ? ? ?顯示編譯錯誤信息
? ? ? ? ?創建一個新的模版實例
? ? ? ? ?用元數據填充模版
? ? ? ? ?輸出結果
下面這段代碼顯示了這些操作:
?
CodeTemplateCompiler compiler = new CodeTemplateCompiler("..//..//StoredProcedures.cst");
compiler.Compile();
?
if (compiler.Errors.Count == 0)
{
? ? CodeTemplate template = compiler.CreateInstance();
?
? ? DatabaseSchema database = new DatabaseSchema(new SqlSchemaProvider(), @"Server=(local)/NetSDK;Database=Northwind;Integrated Security=true;");
? ? TableSchema table = database.Tables["Customers"];
?
? ? template.SetProperty("SourceTable", table);
? ? template.SetProperty("IncludeDrop", false);
? ? template.SetProperty("InsertPrefix", "Insert");
?
? ? template.Render(Console.Out);
}
else
{
? ? for (int i = 0; i < compiler.Errors.Count; i++)
? ? {
? ? ? ? Console.Error.WriteLine(compiler.Errors[i].ToString());
? ? }
}
?
如果你需要提供一個復雜的組合用戶界面來輸入元數據,這時就要添加設計器的支持。換句話說,除此之外沒有別的辦法來輸入你自定義的元數據類型。添加設計器的支持,首先你要創建一個Editor作為自定義的類型,一個Editor其實就一個繼承于.NET 中的System.Drawing.Design.UITypeEditor類的子類。
安裝CodeSmith后在,在C:/Program File/CodeSmith/ SampleProjects 文件夾下有很多SampleCustomProperties的工程。例如:DropDownEditorProperty是一個把字符串和布爾類型的值結合在一起的元數據,它提供了一個類DropDownEditorPropertyEditor繼承于System.Drawing.Design.UITypeEditor。
[Editor(typeof(CodeSmith.Samples.DropDownEditorPropertyEditor), typeof(System.Drawing.Design.UITypeEditor))]
public class DropDownEditorProperty
在使用的時候跟其它的元數據類型是一樣,不過別忘記添加對程序集的引用,引用CodeSmith默認的是不認識該類型的。
<%@ Property Name="DropDownEditorProperty" Type="CodeSmith.Samples.DropDownEditorProperty" Category="Options" Description="This property uses a custom dropdown editor." %>
<%@ Assembly Name="SampleCustomProperties" %>
當用戶想要編輯DropDownEditProperty時,單擊CodeSmith屬性面板將會顯示如下的自定義對話框:
?
在CodeSmith中,如果生成的代碼是SQL腳本,則可以在生成代碼完成時自動執行生成的代碼,也就是在生成的SQL腳本的同時在數據庫中創建新的對象。
用BaseTemplates.ScriptUtility對象提供ExecuteScript方法可以實現,如果想在生成代碼完成后立即執行生成的腳本,可以很方便的通過重載OnPostRender來實現。
在使用之前,先添加對下列程序集的引用:
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
看下面的這個例子:
?
protected override void OnPostRender(string result)?
{
? ? // execute the output on the same database as the source table.
? ? CodeSmith.BaseTemplates.ScriptResult scriptResult =?
? ? ?CodeSmith.BaseTemplates.ScriptUtility.ExecuteScript(this.SourceTable.Database.ConnectionString,?
? ? ?result, new System.Data.SqlClient.SqlInfoMessageEventHandler(cn_InfoMessage));?
? ? Trace.Write(scriptResult.ToString());
? ? base.OnPostRender(result);
}
在這個例子中SourceTable是一個類型為SchemaExplorer.TableSchema.的屬性,使用的時候需要調整部分代碼來獲取數據庫的連接以便在生成代碼完成后執行腳本。
?
在CodeSmith中使用CodeTemplateInfo可以獲取當前模版的一些信息:
屬性
返回值
CodeBehind ??
Gets the full path to the code-behind file for the template (or an empty string if there is no code-behind file).
ContentHashCode
Gets the hash code based on the template content and all template dependencies. ?
DateCreated
Gets the date the template was created.
DateModified
Gets the date the template was modified. ?
Description
Gets the description. ?
DirectoryName
Gets the name of the directory the template is located in. ?
FileName
Gets the name of the template file. ?
FullPath
Gets the full path to the template. ?
Language
Gets the template language. ?
TargetLanguage
Gets the target language. ?
看一下一個具體的使用例子:
?
<%@ CodeTemplate Language="VB" TargetLanguage="Text" Description="Demonstrates CodeTemplateInfo." %>
<% DumpInfo() %>
<script runat="template">
Public Sub DumpInfo()
? ? Response.WriteLine("Template: ? ? ? ?{0}", Me.CodeTemplateInfo.FileName)
? ? Response.WriteLine("Created: ? ? ? ? {0}", Me.CodeTemplateInfo.DateCreated)
? ? Response.WriteLine("Description: ? ? {0}", Me.CodeTemplateInfo.Description)
? ? Response.WriteLine("Location: ? ? ? ?{0}", Me.CodeTemplateInfo.FullPath)
? ? Response.WriteLine("Language: ? ? ? ?{0}", Me.CodeTemplateInfo.Language)
? ? Response.WriteLine("Target Language: {0}", Me.CodeTemplateInfo.TargetLanguage)
End Sub
</script>
執行該模版輸出如下(環境不同,輸出也不同):
Template: ? ? ? ?CodeTemplateInfo.cst
Created: ? ? ? ? 6/29/2005 8:54:19 PM
Description: ? ? Demonstrates CodeTemplateInfo.
Location: ? ? ? ?C:/Program Files/CodeSmith/v3.0/SampleTemplates/Test/CodeTemplateInfo.cst
Language: ? ? ? ?VB
Target Language: Text
?
Progress對象可以在CodeSmith生成代碼時給用戶顯示一個進度條,當生成代碼的時間很長時非常有用。如果你使用的是CodeSmith Explorer,進度條將顯示在Generate按鈕的左邊:
如果使用的是CodeSmith Studio,進度條將顯示在狀態欄上:
使用Progress和在WinForm中使用進度條差不多,需要設置它的最大值和步長:
this.Progress.MaximumValue = 25;
this.Progress.Step = 1;
如果想顯示出進度,需要調用PerformStep方法:
this.Progress.PerformStep();
?
在CodeSmith中,以下幾個快捷鍵有助于我們快速輸入。
1.Ctrl + Shift + C
在空行上,按下Ctrl + Shift + C后將會錄入一個代碼塊。
<% ?%>
?2.Ctrl + Shift + Q
按下Ctrl + Shift + Q后錄入一個腳本塊。
?
<script runat="template">
</script>
3.Ctrl + Shift + V
對代碼塊反轉,如有下面這樣一行代碼:
<% for(int i=0;i<10;i++){}%>
在兩個大括號之間按下Ctrl + Shift + V后,將變成如下代碼:
<% for(int i=0;i<10;i++){%> ?<%}%>
4.Ctrl + Shift + W
按下Ctrl + Shift + W后會錄入一個輸出的代碼塊:
<%= ?%>
注意:在使用快捷鍵的時候,如果想要把一段代碼之間放在錄入的標記中間,首先選中這些代碼,再按下快捷鍵組合。比如我們有一段這樣的代碼,想把它放在<script>里面。
?
public enum CollectionTypeEnum
{
? Vector,
? HashTable,
? SortedList
}
public override void Render(TextWriter writer)
{
? ? StreamWriter fileWriter1 = new StreamWriter(@"C:/test1.cs", true);
? ? this.Response.AddTextWriter(fileWriter1);
? ? StreamWriter fileWriter2 = new StreamWriter(@"C:/test2.cs", true);
? ? ?this.Response.AddTextWriter(fileWriter2);
? ? base.Render(writer);
? ? fileWriter1.Close();
? ? fileWriter2.Close();
}
選中它,再按下Ctrl + Shift + Q后就會變成:
<script runat="template">
public enum CollectionTypeEnum
{
? Vector,
? HashTable,
? SortedList
}
public override void Render(TextWriter writer)
{? ? StreamWriter fileWriter1 = new StreamWriter(@"C:/test1.cs", true);
? ? this.Response.AddTextWriter(fileWriter1);
? ? StreamWriter fileWriter2 = new StreamWriter(@"C:/test2.cs", true);
? ? ?this.Response.AddTextWriter(fileWriter2);
? ? base.Render(writer);
? ? fileWriter1.Close();
? ? fileWriter2.Close();
}
</script>
========
CodeSmith的基礎模版類
基礎模版類
類型描述:?
Batch ? ? ?
OutputFileCodeTemplate ?模版通過繼承此類能夠在生成過程中把他們的輸出保存到文件中?
ScriptError ? ?在腳本執行中出現一個錯誤
ScriptErrorCollection ??
ScriptResult ? ?一個腳本的運行結果包含一些已經發生的錯誤
ScriptUtility ? ?這個類能用來在數據庫上執行Sql腳本。
SqlCodeTemplate ? 繼承此類的模版當從一個Sql數據源生成代碼時能夠獲得很多有用的幫助方法
StringUtility ? ?多種處理string型的方法
各類型下的成員屬性及方法
Batch Class
屬性
Content ??
LineCount ??
StartLineNumber?
方法
Finalize 在一個對象再次創建之前獲得空閑資源并且執行其他的清空操作
MemberwiseClone 建立現有對象的副本
OutputFileCodeTemplate Class
屬性
CodeTemplateInfo ?得到當前模版的信息
OutputFile ?此屬性用來指定一個保存模版輸出的輸出文件名
Progress ? 提供一種方式匯報模版的執行進程
Response ? 模版輸出返回流。此屬性可以在程序中寫出流
State ? 模版實例的狀態
ValidationErrors ?得到模版的錯誤
方法
CopyPropertiesTo ?把匹配的屬性拷貝到另一個代碼模版實例中
GetCodeTemplateInstance 重載,得到指定模版的實例
GetFileName ?為模版的輸出得到一個默認的名字
GetProperties ? 得到模版的所有屬性
GetProperty ? 得到模版的指定屬性
GetRequiredProperties ?得到模版上所有必要的屬性
GetType ? 得到當前實例類型
ParseDefaultValue ?解析屬性的默認值
SavePropertiesToXml ?以XML保存屬性
SavePropertiesToXmlFile 保存屬性到一個XML文檔
SetProperty ? 重載,保存指定的屬性值
ToString
ScriptError Class
屬性
方法
Finalize 在一個對象再次創建之前獲得空閑資源并且執行其他的清空操作
MemberwiseClone 建立現有對象的副本
ScriptUtility Class
屬性
ConnectionString 執行腳本時使用此連接字符串
Script ? 執行的腳本
方法
ExecuteScript ?重載,執行腳本
SqlCodeTemplate Class
屬性
CodeTemplateInfo ?得到當前模版的信息
OutputFile ?此屬性用來指定一個保存模版輸出的輸出文件名
Progress ? 提供一種方式匯報模版的執行進程
Response ? 模版輸出返回流。此屬性可以在程序中寫出流
State ? 模版實例的狀態
ValidationErrors ?得到模版的錯誤
方法
CopyPropertiesTo ?把匹配的屬性拷貝到另一個代碼模版實例中
GetCamelCaseName Returns a camel cased name from the given identifier.?
GetCodeTemplateInstance 重載,得到指定模版的實例
GetCSharpVariableType 基于給定列返回C#的變量類型
GetFileName ?為模版的輸出得到一個默認的名字
GetMemberVariableDeclarationStatement
? ?重載,返回C#成員變量聲明語句
GetMemberVariableDefaultValue
? ?基于一個列的數據類型返回一個默認值
GetMemberVariableName 為一個給定標示返回一個C#成員變量名
GetProperties ? 得到模版的所有屬性
GetProperty ? 得到模版的指定屬性
GetPropertyName ?返回指定列的公有屬性的名字
GetReaderMethod ?Returns the name of the typed reader method for a given column.?
GetRequiredProperties ?得到模版上所有必要的屬性
GetSpacedName ?Returns a spaced out version of the identifier.
GetSqlDbType ?返回一個給定列的SqlDbType
GetSqlParameterExtraParams
? ?為ADO的參數聲明生成額外的參數
GetSqlParameterStatement
? ?重載,返回給定列的T-Sql的參數聲明
GetSqlParameterStatements
? ?重載,給指定列加一個參數到ADO對象生成一個指定聲明(Generates an assignment statement that adds a parameter to a ADO object for the given column. )
GetValidateStatements 基于某列生成一組確認聲明
IncludeEmptyCheck 確定一個給定列是否可以為空
IncludeMaxLengthCheck 確定一個給定列的類型是否需要最大長度的定義
IsUserDefinedType 確定是否一個給定列用了一個UDT(用戶定義類型)
ParseDefaultValue ?解析屬性的默認值
SavePropertiesToXml ?以XML保存屬性
SavePropertiesToXmlFile 保存屬性到一個XML文檔
SetProperty ? 重載,保存指定的屬性值
========
CodeSmith 使用實例
?StringCollection提供了一種集合的輸入方式,在代碼中,可以用Array的方式來引用。在使用這個類之前,在模版中我們必須添加對CodeSmith.CustomProperties程序集的引用:
<%@ Assembly Name="CodeSmith.CustomProperties" %>
添加完程序集之后,我們就可以使用StringCollection在腳本塊中定義一個屬性:
<%@ Property Name="List" Type="CodeSmith.CustomProperties.StringCollection" Category="Custom" Description="This is a sample StringCollection" %>
執行該模版時,這個屬性將在屬性窗體中顯示為一個按鈕:
單擊按鈕,將會彈出一個String Collection Editor對話框:
當然也可以直接在屬性窗口中編輯StringCollection。
模版代碼如下:
<%@ CodeTemplate Language="C#" TargetLanguage="C#" %>
<%@ Assembly Name="CodeSmith.CustomProperties" %>
<%@ Property Name="List" Type="CodeSmith.CustomProperties.StringCollection" Category="Custom" Description="This is a sample StringCollection" %>
using System;
namespace Test
{ ? ? ??
? ? ? ? ?/** <summary>
? ? ? ? ?/// ? ? 測試StringCollection
? ? ? ? ?/// </summary>
? ? ? ? ?public class Test
? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?public static void Main(string[] args)
? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? ? <%for(int i = 0;i<List.Count;i++){%>
? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine(<%=List[i]%>);
? ? ? ? ? ? ? ? ? ? ? ? ? ? <%}%>
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ?}
}
?
成后的代碼:
using System;
namespace Test
{ ? ? ??
? ? ? ? ?/** <summary>
? ? ? ? ?/// ? ? 測試StringCollection
? ? ? ? ?/// </summary>
? ? ? ? ?public class Test
? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?public static void Main(string[] args)
? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine(Apples);
? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine(Fish);
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ?}
}
? StringCollection的重要屬性和方法:
公共屬性
名稱
?
描述
Count
?
獲取StringCollection中包含的字符串的數目
IsReadOnly
?
獲取用于指示StringCollection是否為只讀的值
IsSynchronized
?
獲取一個值,該值指示對StringCollection 的訪問是否為同步的(線程安全的)
Item
?
獲取或設置指定索引處的元素。在C# 中,該屬性為 StringCollection 類的索引器
SyncRoot
?
獲取可用于同步對StringCollection 的訪問的對象
公共方法
名稱
?
描述
Add
?
將字符串添加到 StringCollection 的末尾
AddRange
?
將字符串數組的元素復制到 StringCollection 的末尾
Clear
?
移除 StringCollection 中的所有字符串
Contains
?
確定指定的字符串是否在 StringCollection 中
CopyTo
?
從目標數組的指定索引處開始,將全部 StringCollection 值復制到一維字符串數組中
IndexOf
?
搜索指定的字符串并返回 StringCollection 內的第一個匹配項的從零開始的索引
Insert
?
將字符串插入 StringCollection 中的指定索引處
Remove
?
從 StringCollection 中移除特定字符串的第一個匹配項
RemoveAt
?
移除 StringCollection 的指定索引處的字符串
========
CodeSmith 實例2
?FileNameEditor類給我們提供了在CodeSmith屬性面板中彈出打開或保存文件對話框的方式,在使用時,首先在模版中得添加對程序集CodeSmith.CustomProperties的引用。然后就可以在模版中定義一個屬性來使用FileNameEditor:
?
?1<script runat="template">
?2
?3private string _userFileName = @"c:/temp/test.txt";
?4
?5?
?6
?7[Editor(typeof(FileNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
?8
?9Category("Custom"), Description("User selected file.")]
10
11?
12
13public string UserFileName
14
15{
16
17 ? ? ?get {return _userFileName;}
18
19 ? ? ?set {_userFileName= value;}
20
21}
22
23</script>
24
25
當我們執行該模版時,在屬性面板中同樣顯示為一個按鈕:
單擊該按鈕,彈出一個保存文件的對話框:
我們也可以通過FileDialogAttribute來自定義彈出的對話框,修改模版為:
?
?1private string _openFileName = @"c:/temp/test.txt";
?2
?3
?4[Editor(typeof(FileNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
?5
?6FileDialogAttribute(FileDialogType.Open, Title="Select Input File"),
?7
?8Category("Custom"), Description("User selected file.")]
?9
10?
11
12public string OpenFileName
13
14{
15
16 ? ? ?get {return _openFileName;}
17
18 ? ? ?set {_openFileName= value;}
19
20}
21
22
彈出的對話框如下所示:
當我們想用一個文件夾的名稱來代替文件時,可以使用FolderNameEditor類。
?
?1<%@ Assembly Name="System.Design" %>
?2<script runat="template">
?3private string _outputDirectory = @"c:/temp";
?4[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
?5Category("Custom"), Description("Output directory.")]
?6public string OutputDirectory
?7{
?8 ? ? ? get {return _outputDirectory;}
?9 ? ? ? set {_outputDirectory= value;}
10}
11</script>
12
13
FileNameEditor重要方法和屬性介紹:
公共方法:
名稱
描述
EditValue
使用由 GetEditStyle 方法提供的編輯器樣式編輯指定的對象
GetEditStyle
獲取 EditValue 方法所使用的編輯樣式
========
CodeSmith應用實例(一)
版權聲明:本文為博主原創文章,未經博主允許不得轉載。
一、一個簡單的例子
?
? ? ? ?這個例子僅是一個簡單的應用,在我翻譯并學習完CodeSmith的英文幫助文檔后,對CodeSmith有了一定的了解,開始著手編寫一些CodeSmith應用模板,今天按照最早提到的例子自行編寫了一個基于表的添加存儲過程的生成模板。具體語法前面基礎中已做過詳細解釋這里僅是一個小綜合應用的例子,望對大家學習CodeSmith有很好的幫助。我的同事也寫了幾個CodeSmith的技巧的文章http://terrylee.cnblogs.com/大家有空去看看,寫的很不錯哦,都是針對于CodeSmith自定義屬性編寫的東東:)
?1<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL" Description="Create a procedure which have insert function base on a table." %>
?2<%@ Assembly Name="SchemaExplorer" %>
?3<%@ Import Namespace="SchemaExplorer" %>
?4<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="DataTable" Description="Table that the stored procedures should be based on." %>
?5<%@ Property Name="Author" Type="String" Category="Context" Description="The author for this procedure."%>
?6<%@ Property Name="Description" Type="String" Category="Context" Description="The description for this procedure."%>
?7<script runat="template">
?8public string GetSqlParameterStatement(ColumnSchema column)
?9{
10 ? ?string param = "@" + column.Name + " " + column.NativeType;
11 ? ?switch (column.DataType)
12 ? ?{
13 ? ? ? ?case DbType.Decimal:
14 ? ? ? ?{
15 ? ? ? ? ? ?param += "(" + column.Precision + ", " + column.Scale + ")";
16 ? ? ? ? ? ?break;
17 ? ? ? ?}
18 ? ? ? ?default:
19 ? ? ? ?{
20 ? ? ? ? ? ?if (column.Size > 0)
21 ? ? ? ? ? ?{
22 ? ? ? ? ? ? ? ?param += "(" + column.Size + ")";
23 ? ? ? ? ? ?}
24 ? ? ? ? ? ?break;
25 ? ? ? ?}
26 ? ?}
27 ? ?return param;
28}
29</script>
30CREATE PROCEDURE dbo.<%=SourceTable.Name %>Insert
31/*
32==================================================
33Author:<%= Author %>
34CreatedTime:<%= System.DateTime.Now.ToShortDateString() %>
35Description:<%= Description %>
36==================================================
37*/
38<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
39<%= GetSqlParameterStatement(SourceTable.Columns[i]) %><% if (i < SourceTable.Columns.Count - 1) { %>,<% } %> ? ?<% if (SourceTable.Columns[i].Description != "") { %>--<%= SourceTable.Columns[i].Description %><% } %>
40<% } %>
41AS
42Insert Into [<%= SourceTable.Name %>]?
43(
44<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
45[<%= SourceTable.Columns[i].Name %>]<% if (i < SourceTable.Columns.Count - 1) { %>,<% } %> ? ?<% if (SourceTable.Columns[i].Description != "") { %>--<%= SourceTable.Columns[i].Description %><% } %>
46<% } %>
47)
48Values
49(
50<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
51@<%= SourceTable.Columns[i].Name %><% if (i < SourceTable.Columns.Count - 1) { %>,<% } %>
52<% } %>
53)
二、具有刪除功能的模板
? ? ? ? 今天又根據CodeSmith的幾個基本組件寫出了基于表生成刪除功能的存儲過程代碼生成模板。
? ? ? ? 昨天覺得添加的存儲過程模板寫的比較簡單,今天準備詳細介紹一下這個刪除的模板。
? ? ? ? 首先介紹我們使用到的一個教本函數GetSqlParameterStatement(ColumnSchema column),其函數代碼如下:
?1public string GetSqlParameterStatement(ColumnSchema column)
?2{
?3 ? ?string param = "@" + column.Name + " " + column.NativeType;
?4 ? ?switch (column.DataType)
?5 ? ?{
?6 ? ? ? ?case DbType.Decimal:
?7 ? ? ? ?{
?8 ? ? ? ? ? ?param += "(" + column.Precision + ", " + column.Scale + ")";
?9 ? ? ? ? ? ?break;
10 ? ? ? ?}
11 ? ? ? ?default:
12 ? ? ? ?{
13 ? ? ? ? ? ?if (column.Size > 0)
14 ? ? ? ? ? ?{
15 ? ? ? ? ? ? ? ?param += "(" + column.Size + ")";
16 ? ? ? ? ? ?}
17 ? ? ? ? ? ?break;
18 ? ? ? ?}
19 ? ?}
20 ? ?return param;
21}
? ? ? ? 大家可以看到,這個函數需要傳入一個ColumnSchema類型的參數,它代表一個數據表中的列,并且是一個列,然后根據ColumnSchema這個類具有的屬性,對傳入的列進行一些操作然后返回我們生成存儲過程時需要的代碼。
? ? ? ? 首先介紹一下ColumnSchema的一些常用屬性,如下表:?
屬性Property
描述Description
AllowDBNull
是否允許空值NULL
Database
通過DatabaseSchema對象得到當前列所屬的數據庫
DataType
此數據對象的數據類型
Description
當前對象的描述
ExtendedProperties
用來存儲SchemaObject的其他附加信息
IsForeignKeyMember
當前列是否為外鍵
IsPrimaryKeyMember
當前列是否為主鍵
IsUnique
當前列是否唯一
Name
列的名稱
NativeType
列定義的數據類型
Precision
數據對象的精度
Scale
數據對象的范圍(個人理解為需要保留小數的范圍)
Size
數據對象的大小(例如:字符串長度為10)
SystemType
數據對象的系統類型
Table
當前列所屬的數據表
? ? ? ? 下面為我們首先要生成存儲過程,要自動生成的代碼分成了紅、綠、藍三部分。
CREATE PROCEDURE dbo.CustomersDelete
/*
==================================================
Author:Bear-Study-Hard
CreatedTime:2005-12-28
Description:Delete a record from table Customers
==================================================
*/
@CustomerID nchar(5) --客戶ID
AS
Delete From [Customers]
Where
[CustomerID] = @CustomerID
? ? 我們的這個腳本函數就是要實現拼出紅色的部分,GetSqlParameterStatement函數接收到ColumnSchema類型的參數后,從其Name屬性和NativeType屬性拼出@CustomerID nchar部分,然后由于不同的數據類型尤其是數值類型和字符串類型的區別,會導致數據類型的大小會有所不同,這里僅對Decimal的數據類型進行了判斷(Numeric和float等均需要這種處理),然后根據Precision屬性得到精度并通過Scale屬性得到了需要保留小數的范圍。如果傳出的為非Decimal類型字段則直接通過Size屬性取出其大小即可。得到了(5)部分。最后的注釋是為了生成的存儲過程解讀性好加上的,使用的是Description屬性。
? ? 剩下的綠色部分和藍色部分生成時比較簡單,請各位自行學習。模板代碼為:
?1<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL" Description="Create a procedure which have delete function base on a table.Must use PrimaryKey to delete a record." %>
?2<%@ Assembly Name="SchemaExplorer" %>
?3<%@ Import Namespace="SchemaExplorer" %>
?4<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="DataTable" Description="Table that the stored procedures should be based on." %>
?5<%@ Property Name="Author" Type="String" Category="Context" Description="The author for this procedure." Optional="true"%>
?6<%@ Property Name="Description" Type="String" Category="Context" Description="The description for this procedure." Optional="true"%>
?7<script runat="template">
?8public string GetSqlParameterStatement(ColumnSchema column)
?9{
10 ? ?string param = "@" + column.Name + " " + column.NativeType;
11 ? ?switch (column.DataType)
12 ? ?{
13 ? ? ? ?case DbType.Decimal:
14 ? ? ? ?{
15 ? ? ? ? ? ?param += "(" + column.Precision + ", " + column.Scale + ")";
16 ? ? ? ? ? ?break;
17 ? ? ? ?}
18 ? ? ? ?default:
19 ? ? ? ?{
20 ? ? ? ? ? ?if (column.Size > 0)
21 ? ? ? ? ? ?{
22 ? ? ? ? ? ? ? ?param += "(" + column.Size + ")";
23 ? ? ? ? ? ?}
24 ? ? ? ? ? ?break;
25 ? ? ? ?}
26 ? ?}
27 ? ?return param;
28}
29</script>
30CREATE PROCEDURE dbo.<%=SourceTable.Name %>Delete
31/*
32==================================================
33Author:<%= Author %>
34CreatedTime:<%= System.DateTime.Now.ToShortDateString() %>
35Description:<%= Description %>
36==================================================
37*/
38<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
39<%= GetSqlParameterStatement(SourceTable.PrimaryKey.MemberColumns[i]) %><% if (i < SourceTable.PrimaryKey.MemberColumns.Count - 1) { %>,<% } %> ? ?<% if (SourceTable.Columns[i].Description != "") { %>--<%= SourceTable.Columns[i].Description %><% } %>
40<% } %>
41AS
42Delete From [<%= SourceTable.Name %>]?
43Where
44<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
45<% if (i > 0) { %>AND <% } %>[<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>
46<% } %>
? ? 如果有問題我會盡力幫助大家解決的,共同提高^_^
本文永久地址: http://www.livebaby.cn/blog/u/meil/archives/2007/984.html
?
========
CodeSmith使用基礎教程 四 — 控制臺與屬性編輯器
七、CodeSmith控制臺指南。
很多人僅僅知道CodeSmith像一個圖形應用程序,或者可能是一個Visual Studio的附件,但是通過CodeSmith的控制臺應用程序還有好多其他的使用方法。控制臺應用程序是很有價值的,因為可以通過它去生成腳本,或者其他一些自動工具。這篇文檔的目的就是要告訴你怎樣使用它的控制臺應用程序并且如何去定義變量和參數。
Basic Usage
大多數情況下是用控制臺應用程序來創建一個模板,一個屬性文件,然后保存輸出的文件。這有一個很好的例子介紹將合并模版的處理過程放到一個過程中,就像使用NAnt工具。
首先我們要確定完成一個什么樣的模版,為這個模板創建一個什么樣的XML屬性文件。XML屬性文件提供在執行模版是需要的各個屬性。生成一個屬性文件最簡單的方法是在CodeSmith Explorer中打開一個模版,填寫屬性,點擊生成按鈕generate,然后再點擊Save Property Set XML按鈕。這個按鈕會在點擊完生成按鈕后找到,在Save Output和Copy Output按鈕旁邊。然后系統提示輸入保存XML屬性文件的文件名,下面看一個ArrayList.cst模版創建的XML屬性文件。
?1<?xml version="1.0" encoding="us-ascii"?>
?2<codeSmith>
?3 ? ? ? ? ? ?<propertySet>
?4 ? ? ? ? ? ? ? ? ? ? ? ?<property name="Accessibility">Public</property>
?5 ? ? ? ? ? ? ? ? ? ? ? ?<property name="ClassName">PersonArray</property>
?6 ? ? ? ? ? ? ? ? ? ? ? ?<property name="ItemType">Person</property>
?7 ? ? ? ? ? ? ? ? ? ? ? ?<property name="ItemValueType">False</property>
?8 ? ? ? ? ? ? ? ? ? ? ? ?<property name="ItemCustomSearch">False</property>
?9 ? ? ? ? ? ? ? ? ? ? ? ?<property name="KeyName">PersonID</property>
10 ? ? ? ? ? ? ? ? ? ? ? ?<property name="KeyType">int</property>
11 ? ? ? ? ? ? ? ? ? ? ? ?<property name="IncludeInterfaces">True</property>
12 ? ? ? ? ? ? ? ? ? ? ? ?<property name="IncludeNamespaces">False</property>
13 ? ? ? ? ? ?</propertySet>
14</codeSmith>
就像看到的一樣,也可以手動創建這個文件,但是使用CodeSmith Explorer會更簡便。
現在我們有了這個XML文件,我們繼續看一下如何去執行這個模版并是用控制臺工具保存結果。首先我們需要是用/template參數去聲明我們要是用的模版,像這樣:
C:/Program Files/CodeSmith/v3.0>cs /template:Samples/Collections/ArrayList.cst
在這個例子中我們使用了ArrayList.cst模版,它存儲在本地的Samples/Collections文件夾下。下一步我們要去聲明我們在最后一步需要創建的XML文件,我們是用/propertyset參數去實現。
C:/Program Files/CodeSmith/v3.0>cs /template:Samples/Collections/ArrayList.cst ?/propertyset:PersonArray.xml
這個/property參數用來指定我們的XML屬性文件。最后一個我們需要用的參數是/output參數,用來指定輸出怎樣被保存。
C:/Program Files/CodeSmith/v3.0>cs /template:Samples/Collections/ArrayList.cst /propertyset:PersonArray.xml /out:test.cs
使用/out參數指定將結果輸出到一個叫test.cs文件中保存。執行這個命令后,模板將開始運行,使用屬性文件將結果輸出到test.cs文件保存。
這是大多數情況下有效使用控制臺。
Merging Output
在各種代碼生成中最大的挑戰就是將生成的代碼和開發人員編寫或修改的代碼區分開。控制臺對這個問題提供了一個有效的獨特的解決方案,使用一個指定的參數在當前已存在的代碼文件中需要將模板生成的代碼添加的地方指定一塊區域。
下面是一個簡單的代碼文件,包含了我們要添加生成代碼的區域。
1using System;
2
3namespace Entities
4{
5 ? ? ?GeneratedOrderEntity ?
9}
我們的目標是將DatabaseSchema/BusinessObject.cst模版生成的代碼添加到類文件的GeneratedOrderEntity區域中。和上一個例子一樣,使用CodeSmith console控制臺應用程序執行這個模版,但是這次要使用另一個參數merge。
C:/Program Files/CodeSmith/v3.0>cs /template:Samples/DatabaseSchema/BusinessObject.cst /propertyset:OrderEntity.xml /out:OrderEntity.cs /merge:InsertRegion= "RegionName=Sample Generated Region;Language=C#;"
使用merge參數我們可以指定區域的名稱,在這個例子中是GeneratedOrderEntity,然后控制臺應用程序將執行模版,并將結果添加到這個區域中。我們來看一下執行完這個指令后生成的代碼。
?1using System;
?2
?3namespace Infozerk.AuthServices.UnitTestSuite
?4{
?5 ? ? ?GeneratedOrderEntity就像看到的一樣,Order類被添加到了我們指定的區域中。在代碼文件中使用merge參數生成的內容在其他部分被修改或手寫后很容易重新再次生成而不會產生影響。
?
參數介紹Parameter Reference
Specifying Output
/out:<file>
指定從模版創建的輸出文件的名稱。
/out:default
指定這個文件被默認保存成模版是用的名稱。
/merge:<mergetype>=<init>
指定模版輸出的區域。可以簡寫為/m
Specifying Input
/template:<file>
選擇要執行的模版,簡寫為/t
/propertyset:<file>
生成代碼時需要使用的XML屬性文件。簡寫為/p
Compiler Options
/debug[+|-]
指定模版需要包含的調試信息。(允許在運行模版時進行調試)
/tempfiles[+|-]
指定保留臨時文件。(如果在臨時文件上調試也可以)
Miscellaneous
/help
顯示幫助信息。
/nologo
禁止生成器版權信息。
八、編寫CodeSmith自定義屬性的編輯器(Writing Custom Property Editors)
? ? ? ? 當你開始編寫自定義的CodeSmith模板時,很可能對于使用它的strings或integers屬性很滿意,但有時你會發現需要創建一個不同類型的屬性,可能是一個自定義的類型或者是.NET framework中但是在屬性面板中沒有提供的類型。在模板中去作這些很簡單,但是怎樣指定一個類型在運行模板時顯示在屬性面板中呢?例如創建了一個Person類并且具有很多不同的屬性,但是卻沒有辦法讓用戶去組裝這個類……除非創建一個自定義屬性編輯器。
? ? ? ? 屬性面板提供了方法去編寫自定義的屬性編輯器,當用戶在面板上選擇一個屬性時可以激發相應的方法,同時也可以通過編寫代碼實現提示用戶輸入一個必要的值。下面我們舉個例子,創建一個接受組建的屬性并且是用影射循環貫串所有的類,是所有的類都可以使用它和它的方法,去創建一個NUnit測試基礎。(這句翻譯的不好,原文:As an example we are going to build a template which accepts an assembly as a property and then using reflection loops through all of the classes, and the methods of those classes, to build NUnit test stubs.)
? ? ? ? 首先,我們來關注一下模板的組件屬性,暫且不看自定義編寫的代碼。模板的第一部分是一些聲明定義和屬性。將屬性放在腳本標簽中代替使用屬性聲明,在下一部分將看到這樣做的必要。
?1<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Builds a class for each class in the assembly, and a test stub for every method." %>
?2
?3<%@ Import NameSpace="System.Reflection" %>
?4
?5<script runat="template">
?6
?7private Assembly assembly;
?8
?9public Assembly AssemblyToLoad
10{
11 ? ? ?get{return assembly;}
12 ? ? ?set{assembly = value;}
13}
14
15</script>
?
? ? ? ? 然后我們為組建assembly中的每一個類創建一個類,為每一個類創建他的方法。然后直接將模板的輸出內容放入Visual Studio.NET,然后在編寫組件的單元測試時使用向導。
?1using System;
?2using NUnit.Framework;
?3
?4<%
?5 ? ? ?foreach(Type T in AssemblyToLoad.GetTypes())
?6 ? ? ?{
?7 ? ? ? ? ? ?if(T.IsClass)
?8 ? ? ? ? ? ?{
?9 ? ? ? ? ? ? ? ? ?%>
10
11 ? ? ? ? ? ? ? ? ?[TestFixture]
12 ? ? ? ? ? ? ? ? ?public class <%=T.Name%>Tests
13 ? ? ? ? ? ? ? ? ?{
14 ? ? ? ? ? ? ? ? ?<%
15 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?MethodInfo[] methods = T.GetMethods ( BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static ?);
16 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?foreach(MethodInfo M in methods)
17 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{
18 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?%>
19
20 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[Test]
21 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?public void <%=M.Name%>Test
22 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{
23 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//TODO Write this test
24 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ??
25 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<%
26 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
27
28 ? ? ? ? ? ? ? ? ?%>}<%
29 ? ? ? ? ? ?}
30 ? ? ?}
31%>
?/Files/Bear-Study-Hard/AssemblyHelper.zip
? ? ? ? 首先我們需要創建一個繼承UITypeEditor的類。
1public class AssemblyFilePicker : UITypeEditor
2{
3 ? ? ?public AssemblyFilePicker(): base()
4 ? ? ?{
5 ? ? ?}
6}
?
? ? ? ? 關于UITypeEditor的說明請大家參看MSDN或Visual Studio.NET自帶幫助中的說明,其中有詳細的例子。
? ? ? ? 然后我們需要重載UITypeEditor類的兩個不同的方法。第一個需要重載點的方法是GetEditStyle,這個方法是告訴屬性面板對于當前類型是用什么類型的編輯器,在這個例子中我們設置編輯類型為Modal。這樣大家可以在該屬性格子的右邊看到一個小按鈕,它將引發一個對話框等模式的對話(trigger a modal dialog)。這是我們的GetEditStyle方法:
1public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)?
2{
3 ? ? ?return UITypeEditorEditStyle.Modal;
4}
?
? ? ? ? 其中的Modal為顯示一個省略號按鈕。
? ? ? ? 首先我們要從當前的服務和控件中得到一個參考,有了控件的參考我們可以通過它轉到ShowDialog方法。(原文:First we need to get a reference to the current service and control, we need the reference to the control so we can pass it to the ShowDialog method.) ? ? ? ? 然后我們創建一個openFileDialog類并填入適合的屬性。 ? ? ? ?然后我們通過控件的參考(reference)將對話框顯示給用戶。 ? ? ? ?下一步我們檢查用戶是否點擊了OK按鈕,如果點擊了,通過文件選擇對話框選擇文件后使用LoadForm方法加載這個組件,最后返回這個值。 ? ? ? ?這個值將被放在屬性面板中并可以被模板讀取,但是需要注意,在我們作這個之前要將組件import引入到模板中,并在模板中用一對屬性聲明。
?
1IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
2Control editorControl = editorService as Control;
3
4if (editorControl != null)?
5{
?
1OpenFileDialog openFileDialog = new OpenFileDialog(); ? ? ? ? ? ? ? ? ? ? ? ??
2
3openFileDialog.CheckFileExists = true;
4openFileDialog.DefaultExt = ".dll";
5openFileDialog.Multiselect = false;
6openFileDialog.Title = "Select an Assembly:";
7openFileDialog.Filter = "Assembly Files | *.dll";
?
1DialogResult result = openFileDialog.ShowDialog(editorControl);
?
?1if (result == DialogResult.OK)
?2 ? ? ? ? ? ?{
?3Assembly assembly = Assembly.LoadFrom( openFileDialog.FileName ) ;
?4 ? ? ? ? ? ? ? ? ?value = assembly;?
?5 ? ? ? ? ? ?}
?6 ? ? ? ? ? ?else
?7 ? ? ? ? ? ?{
?8 ? ? ? ? ? ? ? ? ?value = null;
?9 ? ? ? ? ? ?}
10 ? ? ?}
11}
12
13return value;
14}
?
? ? ? ? 加載這個模板我們僅需將這個組件assembly與模板放在同一目錄下,然后再模板中加入下面兩行代碼。
1<%@ Assembly Name="AssemblyHelper" %>
2<%@ Import NameSpace="AssemblyHelper" %>
?
? ? ? ? 需要重載的另一個方法是EditValue方法,當用戶電擊屬性時會調用這個方法。按照我們需要加載的組件類型需要創建一個打開文件對話框(open file dialog)然后捕獲這個對話框,在屬性格子中返回對話框的結果。
1public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
2{
3
4if (provider != null)?
5{
? ? ? ? 這個模板僅僅可以編譯通過,但是由于我們編寫顯示了一個類型屬性面板并不知道如何去操作它,所以我們沒有辦法自定義指定組件在加載時想要加載的組件。
? ? ? ? 我們需要創建一個UITypeEditor,這是一個建立屬性面板是用的特殊屬性的類。UITypeEditor需要創建在一個和模板分離的組件中,我們是用Visual Studio創建這個類。
本文永久地址: http://www.livebaby.cn/blog/u/meil/archives/2007/982.html
========
實例演示CodeSmith與ECC開發模式結合---快速建站(一)
http://blog.csdn.net/breakersam/article/details/1551935?
前言
? ? ? ?其實本文所說的思路產生以很久以前,本人也曾將用于多個小型項目開發之中,用下來的整體說還是比較滿意。按理說,自己既然用了那么多次也應寫些心得或是總結出來吧,無奈一是自己太懶二是文字功底有限,只好罷了!拖到了現在,方提指敲寫,也不知成文怎樣,由大家評說吧。
? ? ? 本文作為開發筆記以流水的方式敲寫,其中不討論開發模式之間的優劣;如大家有好的建議也希望直面地提出來,我呢,以求進步!
文中所舉的例子:ASP.NET (Net1.1)+ Access 基礎上所搭建。
?
CodeSmith概述
? ? ? ?CodeSmith中文我不知叫什么,不太好解釋,必竟這是“工業化”的東西,有點抽象,顧名思義(Code Smith 代碼工匠),就是生成代碼的東西;CodeSmith作用在于生成代碼模板,減少手工書寫代碼的工作,提高生產效率,增加剝奪收入。習慣上,我們項目組里在應用CodeSmith有這著這樣的感受:C#的語法,ASP的寫法。不過話也得說回來,在ASP時代,我們沒有這樣的“艷遇”,那時代碼的生成,只能在自己編寫ASP Generator里生成,并且編寫過程中也沒有這么好的IDE支持。
? ? ? ? 或許有人會這樣認為,既然有了CodeSmith,那么好多邏輯不是一下子就可以生成了嗎?干嘛要用什么ECC,煩!事實上,這話有部分多,也有大部分不對!是的,CodeSmith能快速地根據模板生成大量的代碼,但不有能生成所有的代碼,因為在一個邏輯必須根據具體的應用環境情況下隨時變動邏輯的代碼,其沒有任何的意義可言。平時在開發過程中,我們也對沒有接觸過的CodeSmith的伙伴這樣說:Code Smith 主要的是生成DAO(特別是與數據庫的表中粒度一一對應的書寫中)與相對穩定的應用對象邏輯代碼,其不是萬能的!生成的代碼必須是軟件開發過程中所已經協商好的開發模式的反映,而不是什么都要CodeSmith生成,否則生成的代碼,誰也看不懂,更不用說維護了或是項目組內成員的學習了。
?
? ? ? ?下面我就簡單提一下CodeSmith初學者經常提到了幾個問題,至于CodeSmith具體是怎樣應用的,我們可以查看其自帶的學習例子或是在網上搜索,教程一大把。如下:
1.CodeSmith怎樣與數據庫結合
CodeTemplate里本身已經提供了訪問了數據庫的屬性:
///
數據庫的連接
?<%@ Property Name="SourceTable" ? ? ? ? ? ? ? ? Type="SchemaExplorer.TableSchema" Category="1. Context"
? ? ? ?Description="Table that the stored procedures should be based on." %>
在DataSource里Provider type t選擇ADOXschemaProvider,輸入類似的ConnectionString:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/開發項目/基于ACCESS的小型系統.NET/BreakerSimply/DataBase/BreakerSimply.mdb
//
表的訪問
//writeable columns Data
#region IsWritableColumns
public string IsWritableColumns()
{
//把ID 排除
? ? ? ?string sTemp ="";
? ? ? ?foreach(ColumnSchema column in SourceTable.Columns)
? ? ? ?{
? ? ? ? ? ? ? ? ? ? ?if (column.Name.ToLower()=="id")
? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ?else
? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? ? sTemp +=this.GetPropertyName(column)+",";
? ? ? ? ? ? ? ? ? ? ?}
? ? ? ?}
? ? return sTemp;
}
#endregion
?
public string GetNativeTypeString(ColumnSchema column)
{
? ? ? ?string sColumnType = "";
? ? ? ?string sTemp = column.NativeType.ToString();
? ? ? ?//Debug.Print(sTemp);
? ? ? ?//Response.WriteLine("{0}={1}", column.Name, sTemp);
? ? ? ?//Debugger.Break();
? ? ? ?sTemp = sTemp.ToLower();
? ? ? ?switch(sTemp)
? ? ? ?{
? ? ? ? ? ? ? case "advarwchar":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.VarWChar";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "adinteger":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.Integer";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "addate":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.Date";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "adlongvarwchar":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.VarWChar";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "adboolean":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.Boolean";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "adcurrency":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.Currency";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "float":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.Float";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "image":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.Image";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "int":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.Int";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "money":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.Money";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "nchar":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.NChar";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "ntext":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.NText";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "nvarchar":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.NVarChar";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "real":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.Real";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "smalldatetime":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.SmallDateTime";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "smallint":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.SmallInt";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "smallmoney":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.SmallMoney";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "text":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.Text";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "timestamp":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.Timestamp";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "tinyint":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.TinyInt";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "uniqueidentifier":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.UniqueIdentifier";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "varbinary":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.VarBinary";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "varchar":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.VarChar";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? case "variantariant":
? ? ? ? ? ? ? ? ? ? ?sColumnType = "OleDbType.Variantariant";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? ?sColumnType = "";
? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ?}
? ? ? ?return sColumnType;
}
?
2.CodeSmith生成后的代碼存放。
如下
<script runat="template">
#region Template Property Script
private string _outputDirectory = String.Empty;
?
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
public string OutputFolder
{
?get
?{
? ? if (_outputDirectory.Length == 0)
? ? ? ?{
? ? ? ? ? ? ? string sOutputFolder = this.CodeTemplateInfo.DirectoryName;
? ? ? ? ? ? ? if(!sOutputFolder.EndsWith("//"))
? ? ? ? ? ? ? ? ? ? ?sOutputFolder += "//";
? ? ? ? ? ? ?
? ? ? ? ? ? ? sOutputFolder += "Tempout//Model";
? ? ? ? ? ? ? if(!System.IO.Directory.Exists(sOutputFolder))
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ?System.IO.Directory.CreateDirectory(sOutputFolder);
? ? ? ? ? ? ? }
? ? ? ? ? ? ? return sOutputFolder;
? ? ? ?}
? ? return _outputDirectory;
?}
?set
?{
?if (value.EndsWith("//")) value = value.Substring(0, value.Length - 1);
?_outputDirectory = value;
?}
}
#endregion
</script>
CodeSmith軟件自帶了很多很好的例子,我也是從上面學的~~。
什么ECC
? ? ? ECC由Engine、Class、Collection三部分構成,Engine實際上是個控制器,負責怎樣創建返回Class,Class表示現實世界的實體,具備詳細的屬性,Collection就是裝載Class的集合容器了; ECC 有人稱之為“與實現無關無關的應用框架”,或許這有點夸大之嫌,什么叫做“與實現無關”,暈死。ECC確實提供了松散耦合的體系結構,它的出現使被操作的對像之間更顯得獨立,更為清晰,并且在很大程度上也降低了業務與具體邏輯的耦合
在ASP.NET中,我們還得考慮這么個問題,數據控件的數據綁定問題:控件支持CollectionBase的綁定,支持自定義行為(特別跨對像之間的數據操作);這些都能在ECC的模式里得到良好的解決。
實例中的ECC架構
? ? ?本文將以一個具體的例子進行說明:(本想貼些圖,但發現貼了好幾次都不成功,只好罷了)
? ? ?這個例子主要是實現文章的管理,如分類與相關CRUD,也就是平時所說的做了個小小的新聞發布系統:欄目表(ArticleColumn) 與 Article表,兩者是一對多的關系
ArticleColumn表結構
ID ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 自動編號
UUID ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?UUID?
Name ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 欄目名 ? ?
Class ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 級數
Order ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?排序
CreatedTime ? ? ? ? ? ? ? ? ? ?創建時間
IsActive ? ? ? ? ? ? ? ? ? ? ? ? ? 是否啟用
Article表結構
ID ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 自動編號
UUID ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?UUID?
ColumnID ? ? ? ? ? ? ? ? ? ? ? ?欄目ID ? ? ? ? ? ? ??
Title ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 標題 ??
Content ? ? ? ? ? ? ? ? ? ? ? ? ? 內容
Author ? ? ? ? ? ? ? ? ? ? ? ? ? ? 作者
CreatedTime ? ? ? ? ? ? ? ? ? ?創建時間
IsActive ? ? ? ? ? ? ? ? ? ? ? ? ? 是否啟用
? ? ? 一般情況下,我是按這樣的工作流程:
? ? ?1. ? ? ? ? ? ? ?分析所要設計的系統有幾個可以稱為“對像”的元素;
? ? ?2. ? ? ? ? ? ? ?接著,用toghter畫一下設計下邏輯圖,生成初步的“原始代碼”;
? ? ?3. ? ? ? ? ? ? ?在“原始代碼”,添血加肉,完成初步的邏輯;
? ? ?4. ? ? ? ? ? ? ?以上面的代碼為骨架,在CodeSmith編寫相應的代碼。
? ? ?以純粹文章功能模塊為例:
ArticleMD (與數據庫的ARTICLE表建立一一的映射,將reader讀出數據裝載入Article)
Article (繼承MD,處理關聯的對像數據,如在這里根據文章的ID獲取該文章的欄目)
ArticleDAO (數據庫訪問邏輯,原始的CRUD操作邏輯)
ArticleBL (繼承DAO,根據業務需要,對CRUD的操作進行進一步的封裝)
ArticleCollection 基于CollectionBase,創建Aritlce集合;這個類可以從MSDN的例子獲得。
具體的碼如下:
========
codesmith資料
http://www.cnblogs.com/Terrylee/archive/2005/12/28/306254.htmlCodeSmith開發系列資料總結
http://www.tuicool.com/articles/2EbIra
CodeSmith自己動手寫模板
http://www.cnblogs.com/whitewolf/archive/2010/09/27/1836729.html
CodeSmith模板引擎系列-目錄
http://www.cnblogs.com/wintersun/p/3971072.html
軟件代碼生成之Codesmith模板.netTiers
http://www.cnblogs.com/knowledgesea/p/5016077.html
CodeSmith模板代碼生成實戰詳解
http://blog.csdn.net/gxiangzi/article/details/6865619
CodeSmith .NET三層架構模板
總結
以上是生活随笔為你收集整理的codesmith学习总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windbg内存断点学习总结
- 下一篇: js断点和调试学习总结3