CodeSmith自动生成业务实体类的模板代码
大家都知道用三层架构做网站优点很多,但是要写很多实体业务代码。于是也就产生了帮助我们写这些代码的工具,CodeSmith便是其中优秀的一员。又因为CodeSmith具有模板定制的功能,使用灵活度高深受大家喜爱。
下面是我做的最基本的业务实体生成模板,根据个人爱好的不同可以自由修改,生成符合自己习惯,自己需要的代码。
<%--
Name : HanBingEntity.cst
Author :寒冰凝玉Description: 根据数据库自动生成实体类模板
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Creates a simplistic entity object." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the stored procedures should be based on." %>
<%@ Property Name="NameSpace" Type="System.String" Default="HanBing" Optional="False" Category="" Description="命名空间" %>
<%@ Property Name="Author" Type="System.String" Default="寒冰凝玉" Optional="False" Category="" Description="作者" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
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;
namespace <%=NameSpace%>
...{
/**//// <summary>
/// 描述:<%= SourceTable.Name %>实体信息
/// 作者:<%= Author %>
/// 时间:<%= DateTime.Now.ToString() %>
/// </summary>
[Serializable()]
public class <%= SourceTable.Name %>Entity
...{
//定义的变量与数据库中会员卡类型表字段一一对应
Private Column#region Private Column
<% for (int i = 1; i < SourceTable.Columns.Count; i++) ...{ %>
private <%= SourceTable.Columns[i].SystemType %> <%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %>;
<% } %>
#endregion
/**//// <summary>
/// 空的构造函数
/// </summary>
public <%= SourceTable.Name %>Entity()
...{
}
/**//// <summary>
/// 带参的构造函数
/// </summary>
<% for (int i = 1; i < SourceTable.Columns.Count; i++) ...{ %>
/**//// <param name="<%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %>"><%= SourceTable.Columns[i].Description %></param>
<% } %>
public <%= SourceTable.Name %>Entity(<% for (int i = 1; i < SourceTable.Columns.Count-1; i++) ...{ %><%= SourceTable.Columns[i].SystemType %> <%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %>,<% } %><% for (int i = SourceTable.Columns.Count-1; i < SourceTable.Columns.Count; i++) ...{ %><%= SourceTable.Columns[i].SystemType %> <%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %> <% } %>)
...{
<% for (int i = 1; i < SourceTable.Columns.Count; i++) ...{ %>
this.<%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %> = <%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %>;
<% } %>
}
public - Property#region public - Property
<% for (int i = 1; i < SourceTable.Columns.Count; i++) ...{ %>
/**////<summary>
///<%= SourceTable.Columns[i].Description %>
///</summary>
public <%= SourceTable.Columns[i].SystemType %> <%= StringUtil.ToPascalCase(SourceTable.Columns[i].Name) %>
...{
get
...{
return <%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %>;
}
set
...{
<%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %> = value;
}
}
<% } %>
#endregion
}
}
模板生成代码示例如下
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;
namespace HanBing
...{
/**//// <summary>
/// 描述:MemberInfo实体信息
/// 作者:寒冰凝玉
/// 时间:2008-4-8 19:44:27
/// </summary>
[Serializable()]
public class MemberInfoEntity
...{
//定义的变量与数据库中会员卡类型表字段一一对应
Private Column#region Private Column
private System.Int32 cardID;
private System.String cardNum;
private System.String custName;
private System.String custIdentity;
private System.String custPhone;
private System.String custAddress;
private System.DateTime cardDate;
#endregion
/**//// <summary>
/// 空的构造函数
/// </summary>
public MemberInfoEntity()
...{
}
/**//// <summary>
/// 带参的构造函数
/// </summary>
/// <param name="cardID">会员卡编号(外键)</param>
/// <param name="cardNum">会员卡号</param>
/// <param name="custName">会员姓名</param>
/// <param name="custIdentity">会员身份证号</param>
/// <param name="custPhone">会员电话</param>
/// <param name="custAddress">会员地址</param>
/// <param name="cardDate">办卡时间</param>
public MemberInfoEntity(System.Int32 cardID,System.String cardNum,System.String custName,System.String custIdentity,System.String custPhone,System.String custAddress,System.DateTime cardDate )
...{
this.cardID = cardID;
this.cardNum = cardNum;
this.custName = custName;
this.custIdentity = custIdentity;
this.custPhone = custPhone;
this.custAddress = custAddress;
this.cardDate = cardDate;
}
public - Property#region public - Property
/**////<summary>
///会员卡编号(外键)
///</summary>
public System.Int32 CardID
...{
get
...{
return cardID;
}
set
...{
cardID = value;
}
}
/**////<summary>
///会员卡号
///</summary>
public System.String CardNum
...{
get
...{
return cardNum;
}
set
...{
cardNum = value;
}
}
/**////<summary>
///会员姓名
///</summary>
public System.String CustName
...{
get
...{
return custName;
}
set
...{
custName = value;
}
}
/**////<summary>
///会员身份证号
///</summary>
public System.String CustIdentity
...{
get
...{
return custIdentity;
}
set
...{
custIdentity = value;
}
}
/**////<summary>
///会员电话
///</summary>
public System.String CustPhone
...{
get
...{
return custPhone;
}
set
...{
custPhone = value;
}
}
/**////<summary>
///会员地址
///</summary>
public System.String CustAddress
...{
get
...{
return custAddress;
}
set
...{
custAddress = value;
}
}
/**////<summary>
///办卡时间
///</summary>
public System.DateTime CardDate
...{
get
...{
return cardDate;
}
set
...{
cardDate = value;
}
}
#endregion
}
}
技巧提示:由于CodeSmith不支持中文你会发现模板中的中文在生成的代码中都变成了乱码,这个问题我的解决办法是打开你的CodeSmith Studio中的模板文件之后再把原来不带乱码的模板代码粘贴到在CodeSmith Studio中打开的模板文件的代码编辑区里,然后编译就行了。
补充:asp.net教程,.Net开发