您的位置:首页 > 其它

CodeSmith快速入门之四:模型层的生成

2008-10-22 18:01 246 查看
在【CodeSmith快速入门之三:数据库我来了】中,我们介绍了对数据库的基本访问,在本章将会带大家进行模型层的编写。

首先先要了解模型层(实体层、VO层)的组成,如下所示:

public class 实体名

{

私有字段声明;

构造函数;

公共属性;

}

注:

--私有字段声明:一般是先声明主键,再是非主键字段,骆驼命名法(首字母小写,新单词首字母大写)

--公共属性:一般是先声明主键,再是非主键属性,帕斯卡命名法(首字母大写,新单词首字母大写)

1、创建C#模板并保存,取名为Model.cst

2、添加声明

<%@ CodeTemplate Language="C#" TargetLanguage="Text" ResponseEncoding="UTF-8"%>

<%@ Property Name="Namespace" Type="String" Default="Model" Category="内容" Description="命名空间名称" %>

<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="内容" Description="数据源表" %>

<%@ Property Name="ObjectName" Type="String" Category="注释" Description="对象名称,为生成注释而用" %>

<%@ Assembly Name="SchemaExplorer" %>

<%@ Import Namespace="SchemaExplorer" %>

3、在<script runat="template"></script>中添加常用的方法

1)编写骆驼和帕斯卡命名转换的方法

public string ConvertToPascal(string str)

{

return str.Substring(0,1).ToUpper() + str.Substring(1);

}

public string ConvertToCamel(string str)

{

return str.Substring(0,1).ToLower() + str.Substring(1);

}

2)编写根据传入的表对象获得类名的方法(如果表名后有“s”,则去掉“s”)

public string GetClassName(TableSchema table)

{

string tempTable;

if (table.Name.EndsWith("s"))

{

tempTable = table.Name.Substring(0,table.Name.Length-1);

}

else

{

tempTable = table.Name;

}

return ConvertToPascal(tempTable);

}

3)编写根据类名设置文件名的方法

public override string GetFileName()

{

return GetClassName(SourceTable) + ".cs";

}
注:模型可以运行,也可以直接导出为文件

模型层的模板

<%--

Name: 模型层代码生成模版

Author: Fencer

Description: 根据数据库的内容生成模型层代码

Version: V1.0 新规初成

--%>

<%@ CodeTemplate Language="C#" TargetLanguage="Text" ResponseEncoding="UTF-8"%>

<%@ Property Name="Namespace" Type="String" Default="Model" Category="内容" Description="命名空间名称" %>

<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="内容" Description="数据源表" %>

<%@ Property Name="ObjectName" Type="String" Category="注释" Description="对象名称,为生成注释而用" %>

<%@ Assembly Name="SchemaExplorer" %>

<%@ Import Namespace="SchemaExplorer" %>

using System;

using System.Collections.Generic;

using System.Text;

namespace <%=Namespace%>

{

/// <summary>

/// <%=ObjectName%>的模型

/// </summary>

[Serializable()]

public class <%=GetClassName(SourceTable)%>

{

private <%=GetPrimaryKeyType(SourceTable)%> <%=GetPrimaryKeyFieldName(SourceTable)%>;

<%

// 循环输出非主键列的定义

foreach(ColumnSchema colum in SourceTable.NonPrimaryKeyColumns)

{

%>

private <%=GetDataTypeByColumn(colum)%> <%=GetFieldNameByColumn(colum)%>;

<%

}

%>

public <%= GetClassName(SourceTable)%>() {}

public <%=GetPrimaryKeyType(SourceTable)%> <%=GetPrimaryKeyPropertyName(SourceTable)%>

{

get{ return this.<%=GetPrimaryKeyFieldName(SourceTable)%>; }

set{ this.<%=GetPrimaryKeyFieldName(SourceTable)%> = value; }

}

<%

// 循环输出非主键列的属性

foreach(ColumnSchema colum in SourceTable.NonPrimaryKeyColumns)

{

%>

public <%=GetDataTypeByColumn(colum)%> <%=GetPropertyNameByColumn(colum)%>

{

get{ return this.<%=GetFieldNameByColumn(colum)%>; }

set{ this.<%=GetFieldNameByColumn(colum)%> = value; }

}

<%

}

%>

}

}

<script runat="template">

// 根据表对象获得类名称

public string GetClassName(TableSchema table)

{

string tempTable;

if (table.Name.EndsWith("s"))

{

tempTable = table.Name.Substring(0,table.Name.Length-1);

}

else

{

tempTable = table.Name;

}

return ConvertToPascal(tempTable);

}

// 根据表对象获得主键的类型

public string GetPrimaryKeyType(TableSchema table)

{

if (table.PrimaryKey != null)

{

if (table.PrimaryKey.MemberColumns.Count == 1)

{

return GetCSharpDataTypeByDBColumn(table.PrimaryKey.MemberColumns[0]);

}

else

{

throw new ApplicationException("此模板只支持单个列的主键");

}

}

else

{

throw new ApplicationException("此模板需要有主键的表");

}

}

// 根据表对象获得主键的名称(原始)

public string GetPrimaryKeyName(TableSchema table)

{

if (table.PrimaryKey != null)

{

if (table.PrimaryKey.MemberColumns.Count == 1)

{

return ConvertToCamel(table.PrimaryKey.MemberColumns[0].Name);

}

else

{

throw new ApplicationException("此模板只支持单个列的主键");

}

}

else

{

throw new ApplicationException("此模板需要有主键的表");

}

}

// 根据表对象获得主键的字段名(骆驼命名)

public string GetPrimaryKeyFieldName(TableSchema table)

{

return ConvertToCamel(GetPrimaryKeyName(table));

}

// 根据表对象获得主键的属性名(帕斯卡命名)

public string GetPrimaryKeyPropertyName(TableSchema table)

{

return ConvertToPascal(GetPrimaryKeyName(table));

}

// 根据列对象获得列的类型

public string GetDataTypeByColumn(ColumnSchema column)

{

return GetCSharpDataTypeByDBColumn(column);

}

// 根据列对象获得列的字段名(骆驼命名)

public string GetFieldNameByColumn(ColumnSchema column)

{

return ConvertToCamel(column.Name);

}

// 根据列对象获得列的属性名(帕斯卡命名)

public string GetPropertyNameByColumn(ColumnSchema column)

{

return ConvertToPascal(column.Name);

}

// 根据列对象获得CSharp中类型

public string GetCSharpDataTypeByDBColumn(ColumnSchema column)

{

switch (column.DataType)

{

case DbType.AnsiString: return "string";

case DbType.AnsiStringFixedLength: return "string";

case DbType.Binary: return "byte[]";

case DbType.Boolean: return "bool";

case DbType.Byte: return "byte";

case DbType.Currency: return "decimal";

case DbType.Date: return "DateTime";

case DbType.DateTime: return "DateTime";

case DbType.Decimal: return "decimal";

case DbType.Double: return "double";

case DbType.Guid: return "Guid";

case DbType.Int16: return "short";

case DbType.Int32: return "int";

case DbType.Int64: return "long";

case DbType.Object: return "object";

case DbType.SByte: return "sbyte";

case DbType.Single: return "float";

case DbType.String: return "string";

case DbType.StringFixedLength: return "string";

case DbType.Time: return "TimeSpan";

case DbType.UInt16: return "ushort";

case DbType.UInt32: return "uint";

case DbType.UInt64: return "ulong";

case DbType.VarNumeric: return "decimal";

default:

{

return "__UNKNOWN__" + column.NativeType;

}

}

}

// 帕斯卡命名转换

public string ConvertToPascal(string str)

{

return str.Substring(0,1).ToUpper() + str.Substring(1);

}

// 骆驼命名转换

public string ConvertToCamel(string str)

{

return str.Substring(0,1).ToLower() + str.Substring(1);

}

// 重写获得文件名的方法

public override string GetFileName()

{

return GetClassName(SourceTable) + ".cs";

}

</script>

请关注:【CodeSmith快速入门之五:其他层次的模板

返回目录【CodeSmith快速入门系列
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: