代碼生成器 CodeSmith 的使用(四)

在上一篇的版本中,我們生成了數據庫中的字段,使生成的屬性更加簡潔,可讀性也提高了很多,但都是釷對一個數據庫的單個表,如果要將數據庫中的所有的表都生成相應的類,表中的字段也都生成屬性,運行一次就可以將所有的表中的字段都生成屬性。這樣不僅提高了代碼的生產效率,同時,爲我們省去了很多枯燥乏味的工作,把主要的精力集中在業務的處理上。

接下來就是要生成一個數據庫中所有表中的所有的字段屬性。同樣的,生成後的規則也有三種, Camel規則, Pascal規則,和原生的

首先來看完整的 Camel 規則模板:

 

<%--
Name:  Copyright © Sun 2013-2014 All rights reserved
Contact me:  [email protected]
Author:  SpringFileld
Description: 遍歷數據庫中所有的表,並映射成 類的屬性 Camel 規則
DateTime: 2014-07-31
--%>

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %>
<%@ Property Name="SourceData" Type="SchemaExplorer.DatabaseSchema" Category="Context" Description="Table that the object is based on." %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>

<%foreach( var tb in SourceData.Tables){ %>

    public partial class  <%=StringUtil.ToCamelCase(tb.Name) %>
      {
        <%foreach( var cl in tb.Columns) {%>

         public <%=CSharpAlias[cl.SystemType.FullName]%> <%=StringUtil.ToCamelCase(cl.Name)%> { get; set; }

        <%} %>
      }

<%} %>

生成的效果如下:

 public partial class  accCardAccount
      {

         public int id { get; set; }


         public System.DateTime dateCreate { get; set; }


         public string card { get; set; }


         public decimal balance { get; set; }


         public decimal interest { get; set; }


         public decimal interestRates { get; set; }


         public string remark { get; set; }

      }



    public partial class  accCardComhistory
      {

         public int id { get; set; }


         public System.DateTime dateCreate { get; set; }


         public string cardNo { get; set; }


         public decimal bonus { get; set; }


         public decimal welfare { get; set; }


         public string rechargeCardNo { get; set; }


         public decimal rechargeMoney { get; set; }

      }


生成的效果如下:

 

public partial class  AccCardAccount
      {

         public int Id { get; set; }


         public System.DateTime DateCreate { get; set; }


         public string Card { get; set; }


         public decimal Balance { get; set; }


         public decimal Interest { get; set; }


         public decimal InterestRates { get; set; }


         public string Remark { get; set; }

      }



    public partial class  AccCardComhistory
      {

         public int Id { get; set; }


         public System.DateTime DateCreate { get; set; }


         public string CardNo { get; set; }


         public decimal Bonus { get; set; }


         public decimal Welfare { get; set; }


         public string RechargeCardNo { get; set; }


         public decimal RechargeMoney { get; set; }

      }


 原生的:

<%--
Name:  Copyright © Sun 2013-2014 All rights reserved
Contact me:  [email protected]
Author:  SpringFileld
Description: 遍歷數據庫中所有的表,並映射成類的屬性 Primitive
DateTime: 2014-07-31
--%>

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %>
<%@ Property Name="SourceData" Type="SchemaExplorer.DatabaseSchema" Category="Context" Description="Table that the object is based on." %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>

<%foreach( var tb in SourceData.Tables){ %>

    public partial class  <%=tb.Name %>
      {
        <%foreach( var cl in tb.Columns) {%>

         public <%=CSharpAlias[cl.SystemType.FullName]%> <%=cl.Name%> { get; set; }

        <%} %>
      }


<%} %>


生成的效果:

 public partial class  acc_card_account
      {

         public int Id { get; set; }


         public System.DateTime DateCreate { get; set; }


         public string Card { get; set; }


         public decimal Balance { get; set; }


         public decimal Interest { get; set; }


         public decimal InterestRates { get; set; }


         public string Remark { get; set; }

      }



    public partial class  acc_card_comhistory
      {

         public int Id { get; set; }


         public System.DateTime DateCreate { get; set; }


         public string CardNo { get; set; }


         public decimal Bonus { get; set; }


         public decimal Welfare { get; set; }


         public string RechargeCardNo { get; set; }


         public decimal RechargeMoney { get; set; }

      }


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章