在第一篇中,简单的介绍了 CodeSmith 的使用方法,这次做一个生成简单的数据库字段属性的模板。以下只粘贴主要的代码片段。
<%--
Name: Copyright © Sun 2013-2014 All rights reserved
Contact me: Sunnydayhu@163.com
Author: SpringFileld
Description: 遍历数据库中的表,并映射成类的属性 Camel规则的写法
DateTime: 2014-07-31
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the object is based on." %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %> <% foreach (ColumnSchema column in this.SourceTable.Columns) { %>
private <%= CSharpAlias[column.SystemType.FullName] %> _<%=StringUtil.ToCamelCase(column.Name) %>; public <%= CSharpAlias[column.SystemType.FullName] %> <%= StringUtil.ToCamelCase( column.Name) %>
{
get { return _<%=StringUtil.ToCamelCase(column.Name) %>; }
set { _<%=StringUtil.ToCamelCase(column.Name) %> = value; }
} <% } %>
生成的效果如下:(Camel规则的写法)
private string _sheetNo; public string sheetNo
{
get { return _sheetNo; }
set { _sheetNo = value; }
} private decimal _sheetAmt; public decimal sheetAmt
{
get { return _sheetAmt; }
set { _sheetAmt = value; }
} private System.DateTime _operDate; public System.DateTime operDate
{
get { return _operDate; }
set { _operDate = value; }
} private string _settleFlag; public string settleFlag
{
get { return _settleFlag; }
set { _settleFlag = value; }
} private string _transNo; public string transNo
{
get { return _transNo; }
set { _transNo = value; }
}
Pascal 规则模板代码:
<%--
Name: Copyright © Sun 2013-2014 All rights reserved
Contact me: Sunnydayhu@163.com
Author: SpringFileld
Description: 遍历数据库中的表,并映射成类的属性 Pascall 规则
DateTime: 2014-07-31
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the object is based on." %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %> <% foreach (ColumnSchema column in this.SourceTable.Columns) { %>
private <%= CSharpAlias[column.SystemType.FullName] %> _<%=StringUtil.ToPascalCase(column.Name) %>; public <%= CSharpAlias[column.SystemType.FullName] %> <%= StringUtil.ToPascalCase( column.Name) %>
{
get { return _<%=StringUtil.ToPascalCase(column.Name) %>; }
set { _<%=StringUtil.ToPascalCase(column.Name) %> = value; }
} <% } %>
Pascal 规则生成结果:
private string _SheetNo; public string SheetNo
{
get { return _SheetNo; }
set { _SheetNo = value; }
} private decimal _SheetAmt; public decimal SheetAmt
{
get { return _SheetAmt; }
set { _SheetAmt = value; }
} private System.DateTime _OperDate; public System.DateTime OperDate
{
get { return _OperDate; }
set { _OperDate = value; }
} private string _SettleFlag; public string SettleFlag
{
get { return _SettleFlag; }
set { _SettleFlag = value; }
} private string _TransNo; public string TransNo
{
get { return _TransNo; }
set { _TransNo = value; }
}
原生的属性写法(生成的属性字段的大小写与数据库中的字段的大小写完全相同)
<%--
Name: Copyright © Sun 2013-2014 All rights reserved
Contact me: Sunnydayhu@163.com
Author: SpringFileld
Description: 遍历数据库中的表,并映射成类的属性 原生的写法
DateTime: 2014-07-31
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the object is based on." %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %> <% foreach (ColumnSchema column in this.SourceTable.Columns) { %>
private <%= CSharpAlias[column.SystemType.FullName] %> _<%= column.Name %>; public <%= CSharpAlias[column.SystemType.FullName] %> <%= column.Name %>
{
get { return _<%= column.Name %>; }
set { _<%= column.Name %> = value; }
} <% } %>
原生的属性写法生成的效果:
private string _sheet_no; public string sheet_no
{
get { return _sheet_no; }
set { _sheet_no = value; }
} private decimal _sheet_amt; public decimal sheet_amt
{
get { return _sheet_amt; }
set { _sheet_amt = value; }
} private System.DateTime _oper_date; public System.DateTime oper_date
{
get { return _oper_date; }
set { _oper_date = value; }
} private string _settle_flag; public string settle_flag
{
get { return _settle_flag; }
set { _settle_flag = value; }
} private string _trans_no; public string trans_no
{
get { return _trans_no; }
set { _trans_no = value; }
}
总结:
如果要改成 Pascal 规则的写法,
只要将StringUtil.ToCamelCase(column.Name) 改成
StringUtil.ToPascalCase(column.Name)即可,
若不想用这两种规则,生成原生的字段属性,可以写成column.Name 去掉
StringUtil.ToCamelCase() 和 StringUtil.ToPascalCase() 方法