CodeSmith 建立Ado.Net自訂模版(二)

來源:互聯網
上載者:User

 

 

 

CodeSmith 建立Ado.Net自訂模版(二)

 

接第一篇:  CodeSmith 建立Ado.Net自訂模版(一)

 

 

建立第二個C# Template: Step2_Model.cst(實體類模版)

 

代碼:

<%@ CodeTemplate Language="C#" TargetLanguage="C#" ResponseEncoding="UTF-8" Description="實體類" %>
<%@ Property Name="NameSpace" Type="System.String" Default="Model" Category="Property" Description="命名空間" %>
<%@ Property Name="Author" Type="System.String" Default="Wilson" Category="Property" Description="作者名" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Optional="True" Category="db" Description="表對應檔" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>

//---------------------上面這些就不做解釋了,詳情看CodeSmith 建立Ado.Net自訂模版(一)--------------------------

<script runat="template"> //自訂方法可以寫在這個標籤內
///<summary>
///把資料庫類型轉化為C#類型
///</summary>
public string DataType2CSharpType(System.Data.DbType dbType) //這裡如果有遺漏,大家可以自行補上
    {
        switch (dbType)
        {
            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.DateTime2:
                return "DateTime";
            case DbType.DateTimeOffset:
                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 "DateTime";               
            case DbType.UInt16:
                return "ushort";
            case DbType.UInt32:
                return "uint";
            case DbType.UInt64:
                return "ulong";
            case DbType.VarNumeric:
                return "decimal";
            case DbType.Xml:
                return "string";
            default:
                return "object";
        }
    }
</script>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace <%=NameSpace%>
{
///<summary>
/// 功能: 實體類 (<%=SourceTable.Description%>) //這裡是你資料庫中Description
/// 建立人:<%=Author%> //這裡是上一篇說過的自訂屬性
/// 建立日期:<%=DateTime.Now.ToShortDateString() %> //這裡和上面的<%=%>的用法就是C#的輸出變數
///</summary>
[Serializable]
public class <%=SourceTable.Name%>
{
public <%=SourceTable.Name%>()
{

}

#region##<%=SourceTable.Name%>實體

<%for(int i = 0;i < SourceTable.Columns.Count;i++)%>
<%{%>

///<summary>
///<%=SourceTable.Columns[i].Description%>
///</summary>
public <%=DataType2CSharpType(SourceTable.Columns[i].DataType)%> <%=SourceTable.Columns[i].Name%> {get; set;}

<%}%>

#endregion
}
}

 

DataType2CSharpType方法是把資料庫的類型轉化為C#類型。。。

 

很簡單,幾個標籤己經在CodeSmith 建立Ado.Net自訂模版(一)中做過介紹

<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Optional="True" Category="db" Description="表對應檔" %>

 

有這句話,在屬性欄中可以看到db大目錄下,SourceTable屬性,屬性框中有個按鈕,點擊,會彈出選擇資料庫的視窗

 

或者先在工具的Scheme Explorer工具列中,點擊ManagerDataSource,添加一個資料庫進來。。

 

這樣你就可以選擇資料庫表

 

<%=SourceTable.Name%>  :這是你選擇的表名

<%=SourceTable.Description%> :這是你資料庫中對錶的描述

SourceTable.Columns.Count :這是資料庫中的列總數

SourceTable.Columns[i].DataType :是索引為i的列類型

SourceTable.Columns[i].Name  : 是索引用i的列名

 

有必要介始一下下面的一句

<%@ CodeTemplate Language="C#" TargetLanguage="C#" ResponseEncoding="UTF-8" Description="實體類" %>

Language:這是表示你編寫輸出和函數使用的語言 (可以使用C#,VB等語言

TargetLanguage:這是你產生代碼的語言

ResponseEncoding:這是編碼輸出的格式

還可以設定:Debug,Src等屬性

 

PS:CodeSmith Studio中有智能提示,其它屬性,大家可以自己試試

 

 

後面還有兩篇(資料訪問層、商務邏輯層),有興趣的可以看看。

沒有太多內容了,只有實現了,前兩篇如果看完,

編寫一個CodeSmith模版應該是沒有問題了,

 

相關篇張:

 

CodeSmith 建立Ado.Net自訂模版(一)

 

CodeSmith 建立Ado.Net自訂模版(三)

 

CodeSmith 建立Ado.Net自訂模版(四)     PS:第四篇有CodeSmith直接組建檔案夾及檔案的提示,如果需要自行擴充

 

 

 

源碼下載:http://download.csdn.net/source/3535328

源碼下載二:http://files.cnblogs.com/zhongweiv/AdoTemp.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.