图解CodeSmith使用和实用教程一 - 入门和生成MIS项目实体层代码
? ? CodeSmith,也就是傳說中的那個代碼生成工具。
? ? 下載CodeSmith 6.5,可到我網盤下,鏈接在底部。
一 安裝
1 解壓,點擊安裝;
2 協議;
3 選擇目錄;
4 一路next完成。
5 解壓Crack目錄下的內容;
6 執行上一步解開的東西的安裝,必須不要打開CodeSmith,VS20XX; 安裝后看下開始菜單;如下圖;已經裝好;
二 用CodeSmith生成MIS項目的實體層代碼
? ? 實體層代碼,就是對數據庫表的映射部分的代碼,如果表多一些的話,手寫是個相當大的工作量;
1 ?進入CodeSmith,先測試下;在右側選擇一個自帶示例模板,內容如下圖;
2 點擊 Run;生成該CST模板的生成的代碼如下圖;
3 寫生成項目實體層的模板代碼
test.cst:
<%@ CodeTemplate Inherits="CodeTemplate" Language="C#" TargetLanguage="Text" Description="NetTiers main template." Debug="True" ResponseEncoding="UTF-8"%><%-- 注冊實體層Entity模板 --%> <%@ Register Name="EntityTemplate" Template="D:\WinformTier\Entity.cst" MergeProperties="Flase" ExcludeProperties=""%><%-- 數據庫 --%> <%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" DeepLoad="True" Optional="False" Category="01. Getting Started - Required" Description="Database that the tables views, and stored procedures should be based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, the Entire Database will then be generated."%><% //創建實體層Entity類 this.GenerateEntityClasses();Debug.WriteLine("OK"); %><script runat="template">//生成實體Entity類 private void GenerateEntityClasses(){CodeTemplate Template =new EntityTemplate();foreach(TableSchema table in this.SourceDatabase.Tables){string FileDirectory = OutputDirectory +"\\"+ table.Name +".cs";//生成模板Template.SetProperty("Table",table);//文件輸出Template.RenderToFile(FileDirectory,true);Debug.WriteLine(FileDirectory +" 創建成功.");}} </script><script runat="template">//解決方案輸出路徑 private string Directory = String.Empty;[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))] [Optional, NotChecked][DefaultValue("")]public string OutputDirectory { get{return Directory;}set{if (value.EndsWith("\\")) value = value.Substring(0, value.Length -1);Directory = value;} } </script>Entity.cst
<%@ CodeTemplate Inherits="CodeTemplate" Language="C#" TargetLanguage="Text" Description="NetTiers main template." Debug="True" ResponseEncoding="UTF-8"%><%@ Assembly Name="SchemaExplorer"%> <%@ Import Namespace="SchemaExplorer"%><%@ Property Name="Table" Type="TableSchema" DeepLoad="True" Optional="False" Category="01. Getting Started - Required" Description="Database that the tables views, and stored procedures should be based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, the Entire Database will then be generated."%> using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace Entity { publicpartialclass<%= Table.Name%> { <%foreach(ColumnSchema col in Table.Columns){ %> public <%= col.DataType %> <%= col.Name %> { get;set; } <% } %> } }上述代碼中的“D:\WinformTier\Entity.cst”,可改為自己保存cst的路徑;
另外還要添加數據庫連接;添加了之后CodeSmith會自動讀出數據庫中的表的名字和字段名,然后生成代碼;
添加數據源如下圖,跟VS中類似,此處選擇的是SqlSever架構提供者,如果使用別的數據庫略有不同;
在右側屬性面板選中添加的數據源和選擇輸出目錄;
然后點擊Run,Build成功;看下輸出目錄;一堆的實體層代碼文件已經生成;真的是好多好強大啊;打開一個看一下,沒什么問題,如下圖所示??磥碣Y本主義確實有值得我們學習的地方,
codesmith 6.5; 上述模板代碼;Sql Server示例數據庫AdventureWorks_Data.mdf;可到俺網盤下載;
codesmith下載:
http://pan.baidu.com/s/1o63dEHW
示例數據庫:
http://pan.baidu.com/s/1sjynMGX
模板代碼:
http://pan.baidu.com/s/1o6n4Byy
總結
以上是生活随笔為你收集整理的图解CodeSmith使用和实用教程一 - 入门和生成MIS项目实体层代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL Server分页存储过程实践(图
- 下一篇: C#常量和编程实例