CodeSmith實戰(一):產生實體層

來源:互聯網
上載者:User

1.先建立一個產生單個表的實體的模板

<%--
Name:
Author:
Description:
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="" Inherits="OutputFileCodeTemplate" Debug="False" Description="Template description here." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"  Category="Context"  Optional="True" Description="the table name"   %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="CodeSmith.CustomProperties" %>
<%@ Import Namespace="CodeSmith.CustomProperties" %>
namespace Jack.CodeSmith.Test
{
      public class <%=this.SourceTable.Name%>Model
    {
        public <%=this.SourceTable.Name%>()
        {
          //ToDo:   
        }
        <%for(int i=0; i<this.SourceTable.NonPrimaryKeyColumns.Count;i++){%>
            private <%=GetCSharpVariableType(this.SourceTable.NonPrimaryKeyColumns[i])%> m<%=this.SourceTable.NonPrimaryKeyColumns[i].Name%>;
        <%}%>
        <%for(int i=0; i<this.SourceTable.NonPrimaryKeyColumns.Count;i++)
        {                  
            int namelength=this.SourceTable.NonPrimaryKeyColumns[i].Name.Length;
            string colName=this.SourceTable.NonPrimaryKeyColumns[i].Name;%>
            public  <%=GetCSharpVariableType(this.SourceTable.NonPrimaryKeyColumns[i])%> <%=colName.Substring(0,1).ToUpper()%><%=colName.Substring(1,colName.Length-1)%>
            get {return m<%=this.SourceTable.NonPrimaryKeyColumns[i].Name%>;}
            set {m<%=this.SourceTable.NonPrimaryKeyColumns[i].Name%>=value;}
        <%}%>       
    }   
}

<script runat="template">
// Override the OutputFile property and assign our specific settings to it.
[FileDialog(FileDialogType.Save, Title="Select Output File", Filter="C# Files (*.cs)|*.cs", DefaultExtension=".cs")]
public override string OutputFile
{
    get {return base.OutputFile;}
    set {base.OutputFile = value;}
}
</script>
<script runat="template">
public string GetCSharpVariableType(ColumnSchema column)
{
    if (column.Name.EndsWith("TypeCode")) return column.Name;
    switch (column.DataType)
    {
        case DbType.AnsiString: return "string";
        case DbType.AnsiStringFixedLength: return "string";
        case DbType.Binary: return "byte[]";
        case DbType.Boolean: return "bool";
        case DbType.Byte: return "byte";
        case DbType.Currency: return "decimal";
        case DbType.Date: return "DateTime";
        case DbType.DateTime: return "DateTime";
        case DbType.Decimal: return "decimal";
        case DbType.Double: return "double";
        case DbType.Guid: return "Guid";
        case DbType.Int16: return "short";
        case DbType.Int32: return "int";
        case DbType.Int64: return "long";
        case DbType.Object: return "object";
        case DbType.SByte: return "sbyte";
        case DbType.Single: return "float";
        case DbType.String: return "string";
        case DbType.StringFixedLength: return "string";
        case DbType.Time: return "TimeSpan";
        case DbType.UInt16: return "ushort";
        case DbType.UInt32: return "uint";
        case DbType.UInt64: return "ulong";
        case DbType.VarNumeric: return "decimal";
        default:
        {
            return "__UNKNOWN__" + column.NativeType;
        }
    }
}
</script>

下面是代碼格式化後的

 

2. 再建立一個模板,調用剛才建立的子模板

<%--
Name:
Author:
Description:
--%>

<%@ CodeTemplate Language="C#" TargetLanguage="C#"  Inherits=""  Description="This is my first data temple" %>
<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" Category="" Description="Database containing the tables." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="CodeSmith.CustomProperties" %>
<%@ Import Namespace="CodeSmith.CustomProperties" %>
<%@ Assembly Name="System.Design" %>
<%@ Import Namespace=" System.Design" %>
<%@ Register Name="SubTemplate" Template="C:\CodeSmithStudy\GenerateModel.cst" MergeProperties="True" ExcludeProperties="" %>
<%
for(int i=0; i<this.SourceDatabase.Tables.Count;i++)

     SubTemplate  mySub=new SubTemplate();
    mySub.OutputFile=this.OutputDirectory+@"\"+this.SourceDatabase.Tables[i].Name+"Model.cs";
    mySub.SourceTable=this.SourceDatabase.Tables[i];
    mySub.Render(Response);       
}

%>

<script runat="template">
private string _outputDirectory = @"c:\codesmithstudy";
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
Category("Custom"), Description("Output directory.")]
public string OutputDirectory
{
       get {return _outputDirectory;}
       set {_outputDirectory= value;}
}
</script>

格式化後的

 

3. 從屬性視窗選擇要儲存到的檔案夾和資料庫

4.運行,得到如下結果

5. 模板下載

http://files.cnblogs.com/cnblogsfans/template.rar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.