小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

CodeSmith模板代碼生成實戰(zhàn)詳解

 球球圓圓豆豆 2016-07-15

前言

公司項目是基于soa面向服務(wù)的架構(gòu)思想開發(fā)的,項目分解眾多子項目是必然的。然而子項目的架子結(jié)構(gòu)種類也過多的話,就會對后期的開發(fā)維護產(chǎn)生一鍋粥的感覺。為了盡可能的在結(jié)構(gòu)層避免出現(xiàn)這種混亂的現(xiàn)象,我們就做了一個決定,使用一個統(tǒng)一的架子結(jié)構(gòu),讓項目管理變的簡單起來。

這樣一來,結(jié)構(gòu)中各層就會有很多重復(fù)的代碼或者重復(fù)的邏輯出現(xiàn),為啦提高開發(fā)效率,節(jié)約開發(fā)時間,我們采用了codesmith根據(jù)自定義模板,生成代碼功能。讓單表的增刪改查功能從數(shù)據(jù)訪問層到ui展示層一鍵批量生成。下面就開始我的codeSmith模板編寫歷程回顧。

CodeSmith安裝下載

官網(wǎng)地址:http://www.

下載地址:http://www./downloads

我使用的,帶破解注冊工具的codesmith鏈接:http://pan.baidu.com/s/1dDdndsd。

傻瓜式安裝,不做介紹。只不過你安裝完需要很多碼。那么煩啦,就用我百度云里面的。帶注冊軟件,安裝完之后,不要急于打開codesmith,先去用注冊軟件注冊下。

安裝完成,破解成功。

打開codesmith主界面如下。

 

Note:打開新建Csharp template,然后后綴名為cst的就是模板文件,自己寫的模板代碼,就在這種后綴格式的文件中。然后光標放在模板文件中,F(xiàn)5即可生成你要代碼的文件。

寫自己的codesmith模板代碼。

1、自定義參數(shù)模板

Note:從這里我們能看到參數(shù)的聲明,與基本語法的使用規(guī)則,需帶<%%>。熟悉之后,在右下角給參數(shù)賦值,然后光標放入模板中,點擊f5生成代碼,看下,推敲下。

2、遍歷數(shù)據(jù)庫中表的模板

 

Note:圖片展示的是怎么設(shè)置數(shù)據(jù)庫配置

模板代碼如下

復(fù)制代碼
<%--引入c#模板--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create an enum of tables." %>
<%--聲明數(shù)據(jù)庫的參數(shù),在左下角的Database屬性中,選擇要操作的數(shù)據(jù)庫名稱--%>
<%@ Property Category="Database" Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" Optional="False" Description="Database the table enums will come from." %>
<%--引入下面的類庫,操作數(shù)據(jù)庫必備的。不要糾結(jié)加入就行啦。--%>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%--SourceDatabase, 是你選擇數(shù)據(jù)庫的屬性類,涵蓋數(shù)據(jù)庫的名稱,創(chuàng)建時間,字符串鏈接,描述等等,自己可以點點看 --%>
public enum <%=SourceDatabase.Name %>Tables
{
<%-- 遍歷數(shù)據(jù)庫中的表集合 --%>
<% for(int x = 0; x < SourceDatabase.Tables.Count; x++) 
{ 
    TableSchema table = SourceDatabase.Tables[x];
    if (x < SourceDatabase.Tables.Count -1)
        //輸出表名,這里是c#的注釋,不會被寫進生成的代碼中。\t為換行符。
        Response.WriteLine("\t{0},", table.Name);
    else
        Response.WriteLine("\t{0}", table.Name);
}
%>    
}
復(fù)制代碼

3、遍歷數(shù)據(jù)庫表中的字段,聲明并使用自定義函數(shù)

復(fù)制代碼
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %>
<%--聲明數(shù)據(jù)庫表的參數(shù),在左下角的表屬性中,選擇要操作的數(shù)據(jù)庫表--%>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the object is based on." %>
<%--引入system類型轉(zhuǎn)為c#的數(shù)據(jù)類型的映射字典 --%>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%--引入下面的類庫,操作數(shù)據(jù)庫必備的。--%>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%--遍歷數(shù)據(jù)庫表的字段屬性--%>
<% foreach (ColumnSchema column in this.SourceTable.Columns) {  %>
<%--拼接字符串,輸出c#中實體的屬性--%>
public <%= ControlType(CSharpAlias[column.SystemType.FullName]) %> <%= StringUtil.ToPascalCase(column.Name) %>{ get; set; }

<% } %>
<script runat="template">
 //如果類型為int,或datetime類型輸出可空類型
 public string ControlType(object val)
 {
     var ty=val.ToString();
     if(ty=="int")
     {
         return "int?";
     }
     if(ty=="System.DateTime")
     {
         return "System.DateTime?";
     }
     return ty;
 }
</script>
復(fù)制代碼

4、批量生成文件,并指定生成文件位置

 

代碼如下

復(fù)制代碼
<%@ Template Language="C#" TargetLanguage="Text" %>
<%-- 注冊要生成的模板 --%>
<%@ Register Name="TableEnumTemplate" Template="TableEnum.cst" MergeProperties="Flase" ExcludeProperties=""%>
<%@ Register Name="TableClumTemplate" Template="TableProperties.cst" MergeProperties="Flase" ExcludeProperties=""%>

<%--聲明數(shù)據(jù)庫的參數(shù),在左下角的Database屬性中,選擇要操作的數(shù)據(jù)庫名稱--%>
<%@ Property Category="Database" Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" Optional="False" Description="Database the table enums will come from." %>
<%--Type數(shù)據(jù)類型為TableSchema,表明參數(shù)Table是一個表對象。--%>
<%@ Property Name="SourceTable" Type="TableSchema" DeepLoad="True" Optional="False" Category="Table" Description="Table Name"%>
<%-- 執(zhí)行輸出文件的函數(shù) --%>
<% this.OutPutFile(); %>
<script runat="template">
    //輸出文件
    private void OutPutFile()
    {
        //生成列舉表名的模板
        CodeTemplate table =new TableEnumTemplate();
        //指定輸出路徑
        string tableFilePath = OutputDirectory +"\\"+ this.SourceDatabase.Name +".cs";
        //給子模板參數(shù)賦值
        table.SetProperty("SourceDatabase",this.SourceDatabase);
        table.RenderToFile(tableFilePath,true);
        
        //生成列表表字段的模板
        CodeTemplate cloumn =new TableClumTemplate();
        //指定輸出路徑
        string cloumnFilePath = OutputDirectory +"\\"+ this.SourceTable.Name +".cs";
         //給子模板參數(shù)賦值
        cloumn.SetProperty("SourceTable",this.SourceTable);
        cloumn.RenderToFile(cloumnFilePath,true);
    }
    //解決方案輸出路徑
    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>
復(fù)制代碼

好啦,就這么多啦,能滿足我的需求啦。

小結(jié)

如果你在看到本文后有什么疑問,請加入博客左上角群,一起交流學(xué)習(xí)。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多