在【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
"
;
}
注:模型可以运行,也可以直接导出为文件
导出的文件名称默认为:模板名+TargetLanguage指定的语言,如果要修改,就必须重写(override)GetFileName方法
4)因为数据库的数据类型和CSharp中的数据类型是不同的,所以编写根据列对象获得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;
}
}
5)编写主键的相关方法
主键相关的方法
// 根据表对象获得主键的类型
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));
}
6)编写非主键的相关方法
非主键的相关方法
// 根据列对象获得列的类型
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);
}
4、根据需要产生的格式,对方法进行调用(在声明和<script runat="template">之间进行编写)
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; }
}
<%
}
%>
}
}
5、设置好选项,运行模板
显示结果如下:
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
Model
{
///
<summary>
///
员工的模型
///
</summary>
[Serializable()]
public
class
EmployeeInfo
{
private
int
empID;
private
string
loginID;
private
string
password;
private
short
role;
private
string
empName;
public
EmployeeInfo() { }
public
int
EmpID
{
get
{
return
this
.empID; }
set
{
this
.empID
=
value; }
}
public
string
LoginID
{
get
{
return
this
.loginID; }
set
{
this
.loginID
=
value; }
}
public
string
Password
{
get
{
return
this
.password; }
set
{
this
.password
=
value; }
}
public
short
Role
{
get
{
return
this
.role; }
set
{
this
.role
=
value; }
}
public
string
EmpName
{
get
{
return
this
.empName; }
set
{
this
.empName
=
value; }
}
}
}
好了,你是否结果也成功运行出来了,恭喜!下次将会给大家讲解其他层次模板的制作,谢谢!
注:源代码演示和下载
模型层的模板
<%--
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快速入门系列】