国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

一個(gè)簡(jiǎn)單的代碼生成器(T4文本模板運(yùn)用)

2019-11-17 01:36:53
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一個(gè)簡(jiǎn)單的代碼生成器(T4文本

先看看界面吧!

image

簡(jiǎn)約到如此,說(shuō)是代碼生成器,估計(jì)是要被吐槽的。好吧,借用園子里博友的說(shuō)法,這只是一粒粟子,如果你愿意,你能看到代碼生成器的“種子”。

這樣運(yùn)行的!

image

畫(huà)了個(gè)簡(jiǎn)圖已描述這個(gè)簡(jiǎn)單的代碼生成器的工作過(guò)程。下面的介紹將以此圖展開(kāi):

1)讀取數(shù)據(jù)表的信息:從數(shù)據(jù)庫(kù)中讀取數(shù)據(jù)表的信息并轉(zhuǎn)換成要為T(mén)4文本模板引擎提供的數(shù)據(jù)(EntityClassInfo);

2)將要為T(mén)4文本模板引擎提供的數(shù)據(jù)(EntityClassInfo)作為參數(shù)傳遞給T4文本模板引擎(其實(shí)是T4文本模板引擎的宿主,詳見(jiàn)T4文本模板轉(zhuǎn)換過(guò)程);

3)T4文本模板引擎讀取模板;

4)T4文本模板引擎將生成的文本返回給應(yīng)用程序。

代碼:

一、在應(yīng)用程序和代碼中傳遞的參數(shù)的類(lèi)型

1)EntityClassInfo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace EntityInfo
{
    [Serializable]
    public class EntityClassInfo
    {
        public EntityClassInfo(DataTable dt)
        {
            this.ClassName = dt.TableName;
            List<EntityClassPRopertyInfo> ropertyListTemp = new List<EntityClassPropertyInfo>();
           
            foreach (DataColumn dcol in dt.Columns)
            {
                ropertyListTemp.Add(new EntityClassPropertyInfo(dcol));
            }
            this.RopertyList = ropertyListTemp;
            List<EntityClassPropertyInfo> primaryKeyListTemp = new List<EntityClassPropertyInfo>();
            List<EntityClassPropertyInfo> notPrimaryKeyListTemp = new List<EntityClassPropertyInfo>(ropertyListTemp);
            foreach (DataColumn dcol in dt.PrimaryKey)
            {
                primaryKeyListTemp.Add(new EntityClassPropertyInfo(dcol));
                notPrimaryKeyListTemp.Remove(new EntityClassPropertyInfo(dcol));
            }
            this.PrimaryKeyList = primaryKeyListTemp;
            this.NotPrimaryKeyList = notPrimaryKeyListTemp;
        }
        public string ClassName
        {
            get;
            private set;
        }
        public List<EntityClassPropertyInfo> RopertyList
        {
            get;
            private set;
        }
        public List<EntityClassPropertyInfo> PrimaryKeyList
        {
            get;
            private set;
        }
        public List<EntityClassPropertyInfo> NotPrimaryKeyList
        {
            get;
            private set;
        }
    }
}

2)EntityClassPropertyInfo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace EntityInfo
{
    [Serializable]
    public class EntityClassPropertyInfo
    {
        public EntityClassPropertyInfo(DataColumn dcol)
        {
            this.PropertyName = dcol.ColumnName;
            this.PropertyType = dcol.DataType.Name;
            this.IsValueType = false;
            if (dcol.DataType.IsValueType)
            {
                if (dcol.AllowDBNull)
                {
                    this.PropertyType = this.PropertyType + "?";
                }
                else
                {
                    this.IsValueType = true;
                }
            }
        }
        public string PropertyName
        {
            get;
            private set;
        }
        public string PropertyType
        {
            get;
            private set;
        }
        public bool IsValueType
        {
            get;
            private set;
        }
        public override bool Equals(object obj)
        {
            EntityClassPropertyInfo temp = obj as EntityClassPropertyInfo;
            if (this.PropertyName == temp.PropertyName && this.PropertyType == temp.PropertyType)
            {
                return true;
            }
            return false;
        }
        
    }
}

二、模板

1)生成實(shí)體類(lèi)的模板:Entity.tt

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".txt" #>
<#@ import namespace="EntityInfo" #>
<#@ parameter type="EntityInfo.EntityClassInfo" name="entity" #>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// <#= entity.ClassName#> 的摘要說(shuō)明
/// </summary>
public class <#= entity.ClassName#>
{
    public <#= entity.ClassName#>()
    {
            //
            // TODO: 在此處添加構(gòu)造函數(shù)邏輯
            //
    }
<#  foreach(EntityClassPropertyInfo property in entity.RopertyList)
    { #>
    private <#= property.PropertyType#> m_<#= property.PropertyName#>;
<#;
     }
#>
<#  foreach(EntityClassPropertyInfo property in entity.RopertyList)
    { #>
    public  <#= property.PropertyType#>  <#= property.PropertyName#>
    {
        set { m_<#= property.PropertyName#> = value; }
        get { return m_<#= property.PropertyName#>; }
    }
<#;
    }
#>
}

2)生成DAL層的模板:Dataaccess.tt

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".txt" #>
<#@ import namespace="EntityInfo" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ parameter type="EntityInfo.EntityClassInfo" name="entity" #>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySQL.Data.MySqlClient;
using System.Collections.Generic;
/// <summary>
/// <#= entity.ClassName#> 的摘要說(shuō)明
/// </summary>
public class <#= entity.ClassName#>DAL
{
    public <#= entity.ClassName#>DAL()
    {
    }
    #region 私有方法
    #region 根據(jù)實(shí)體類(lèi)獲取MySqlParameter數(shù)組 +MySqlParameter[] FromModel(<#= entity.ClassName#> model)
    private static MySqlParameter[] FromModel(<#= entity.ClassName#> model)
    {
        List<MySqlParameter> parameterLi
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 会东县| 山丹县| 揭东县| 乐山市| 桦甸市| 邹城市| 永和县| 塔河县| 嘉义县| 稷山县| 盐边县| 安吉县| 大荔县| 福清市| 盈江县| 正定县| 突泉县| 平南县| 昆明市| 万安县| 峨边| 略阳县| 北碚区| 奉化市| 望江县| 龙南县| 康平县| 航空| 宜宾县| 铜陵市| 于田县| 孟津县| 沈阳市| 福建省| 新兴县| 闸北区| 墨竹工卡县| 阳曲县| 屯留县| 宝鸡市| 鄢陵县|