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

畫(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)用程序。
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;
}
}
}
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;
}
}
}
<#@ 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#>; }}
<#;
}
#>
}
<#@ 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
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注