代码生成器 CodeSmith 的使用(三)

时间:2024-04-30 13:06:41

在第二篇中,介绍了用 codesmith 生成数据库中的一些字段,可生成的属性不够简洁,这次对上一次的版本进行重构,生成一些简洁的属性访问器。代码如下:

Camel 规则:

<%--
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) { %> public <%= CSharpAlias[column.SystemType.FullName] %> <%= StringUtil.ToCamelCase(column.Name) %> { get; set; } <% } %>

生成的效果如下:

public string sheetNo { get; set; }

public decimal sheetAmt { get; set; }

public System.DateTime operDate { get; set; }

public string settleFlag { get; set; }

public string transNo { get; set; }

这次的属性简洁了很多,代码也很整齐,程序的可读性得到了进一步的提高

Pascal 规则:

<%--
Name: Copyright © Sun 2013-2014 All rights reserved
Contact me: <a target=_blank href="mailto:Sunnydayhu@163.com">Sunnydayhu@163.com</a>
Author: SprngFileld
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) { %> public <%= CSharpAlias[column.SystemType.FullName] %> <%= StringUtil.ToPascalCase(column.Name) %> { get; set; } <% } %>

生成的效果如下:

public string SheetNo { get; set; }

public decimal SheetAmt { get; set; }

public System.DateTime OperDate { get; set; }

public string SettleFlag { get; set; }

public string TransNo { get; set; }

源生,就是生成的属性的大小写与数据库中的字段完全一样

<%--
Name: Copyright © Sun 2013-2014 All rights reserved
Contact me: <a target=_blank href="mailto:Sunnydayhu@163.com">Sunnydayhu@163.com</a>
Author: SprngFileld
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) { %> public <%= CSharpAlias[column.SystemType.FullName] %> <%=column.Name%> { get; set; } <% } %>

生成的效果如下:

public string sheet_no { get; set; }

public decimal sheet_amt { get; set; }

public System.DateTime oper_date { get; set; }

public string settle_flag { get; set; }

public string trans_no { get; set; }