用CodeSmith生成数据库实体类的代码.
在工作中,经常中与数据库层打交道,所以,我一般用一个数据库实体类来代码来进行相应的操作.
而CodeSmith则是一个功能强大的代码生成模板系统,用它可以非常快的写出你自己的模板
我这个代码只是相让大家对CodeSmith如何从数据库的表来生成代码有一个了解.
模板生成的类支持VB.NET和C#语言.
代码如下:
<%@ CodeTemplate Language="VB" TargetLanguage="Text" Src="" Inherits="" Debug="False" Description="Template description here." %>
<%@ Assembly Name="System.Data" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Design" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import NameSpace="System.IO" %>
<%@ Import NameSpace="System.Design" %>
<%@ Property Name="CurDB" Type="SchemaExplorer.DatabaseSchema" Category="Context" Description="当前操作的数据库." %>
<%@ Property Name="CurTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="选择的数据表名称." %>
<%@ Property Name="MakeLanguage" Type="ML" Default="0" Optional="False" Category="Other" Description="设置要生成的操作语言" %>
<%@ Property Name="NameSpaces" Type="String" Default="EagleFly" Category="Other" Description="类文件的名空间" %>
<%--------------------------------模板开始-------------------------------%>
<%If MakeLanguage = ML.VB Then %>
Imports System
Imports System.Data
Imports EagleFlyFCL.Data
Namespace Model
<Serializable()>Public Class <%= CurTable.Name %>Entity
Public Sub New()
'ToDo
End Sub
<%=GetMakeCode(ML.VB)%>
End Class
End Namespace
<% ElseIf MakeLanguage = ML.CSharp Then %>
using System;
using System.Data;
using EagleFlyFCL.Data;
namespace <%=NameSpaces%>.Service.Model
{
[Serializable()]
public class <%= CurTable.Name %>Entity:EntityObjectBase
{
public <%= CurTable.Name %>Entity()
{
// TODO: 在此处添加构造函数逻辑
}
<%=GetMakeCode(ML.CSharp)%>
}
}
<% End If %>
<%--------------------------------模板结束-------------------------------%>
<script runat="template">
#region "Normal"
Public Enum ML
VB = 0
CSharp = 2
End Enum
'得到指定语言生成的代码
Public Function GetMakeCode(ByVal MakeLang As ML) As String
If MakeLang = ML.VB Then
Return GetVBCode()
ElseIf MakeLang = ML.CSharp Then
Return GetCSCode()
End If
End Function
#end region
Private Function FirstToLower(ByVal Str As String) As String
Dim S As String
S = Str.Substring(0, 1).ToLower & Str.Substring(1)
Return S
End Function
Public Function GetVBCode() As String
Dim Field As ColumnSchema : Dim SB As New System.Text.StringBuilder(100)
Dim ColumnName As String : Dim ColumnType As String
Dim FL As String
For Each Field In CurTable.Columns
ColumnName = Field.Name
ColumnType = GetColumnDataType(ML.VB, Field)
FL = FirstToLower(ColumnName)
SB.Append(vbTab & "Private _" & FL & " As " & ColumnType & GetDefaultValue(ColumnType) & vbCrLf)
SB.Append(vbTab & "<ColumnName()> Public Property " & ColumnName & "() As " & ColumnType & vbCrLf)
SB.Append(vbTab & vbTab & "Get" & vbCrLf)
SB.Append(vbTab & vbTab & vbTab & "Return _" & FL & vbCrLf)
SB.Append(vbTab & vbTab & "End Get" & vbCrLf)
SB.Append(vbTab & vbTab & "Set(ByVal Value As " & ColumnType & ")" & vbCrLf)
SB.Append(vbTab & vbTab & vbTab & "_" & FL & " = Value" & vbCrLf)
SB.Append(vbTab & vbTab & "End Set" & vbCrLf)
SB.Append(vbTab & "End Property" & vbCrLf & vbCrLf)
Next
Return SB.ToString
End Function
Public Function GetCSCode() As String
Dim Field As ColumnSchema : Dim SB As New System.Text.StringBuilder(100)
Dim ColumnName As String : Dim ColumnType As String
For Each Field In CurTable.Columns
ColumnName = Field.Name
ColumnType = GetColumnDataType(ML.CSharp, Field)
SB.Append(vbTab & vbTab & "private " & ColumnType & " _" & ColumnName & GetDefaultValue(ColumnType) & vbCrLf)
SB.Append(vbTab & vbTab & "[ColumnName()]public " & ColumnType & " " & ColumnName & vbCrLf)
SB.Append(vbTab & vbTab & "{" & vbCrLf)
SB.Append(vbTab & vbTab & vbTab & "get" & vbCrLf)
SB.Append(vbTab & vbTab & vbTab & "{" & vbCrLf)
SB.Append(vbTab & vbTab & vbTab & vbTab & "return _" & ColumnName & ";" & vbCrLf)
SB.Append(vbTab & vbTab & vbTab & "}" & vbCrLf)
SB.Append(vbTab & vbTab & vbTab & "set" & vbCrLf)
SB.Append(vbTab & vbTab & vbTab & "{" & vbCrLf)
SB.Append(vbTab & vbTab & vbTab & vbTab & "_" & ColumnName & " = value" & ";" & vbCrLf)
SB.Append(vbTab & vbTab & vbTab & "}" & vbCrLf)
SB.Append(vbTab & vbTab & "}" & vbCrLf & vbCrLf)
Next
Return SB.ToString
End Function
Public Function GetDefaultValue(ByVal ColumnType As String) As String
Select Case ColumnType
Case "Integer", "Double"
Return "=0 "
Case "int", "double"
Return "=0; "
Case "String"
Return "= """""
Case "string"
Return "= """";"
Case "DateTime"
If MakeLanguage=ML.VB Then
Return "=System.DateTime.Now "
Else
Return "=System.DateTime.Now;"
End If
Case "Boolean"
Return "=False "
Case "bool"
Return "=false; "
Case Else
If MakeLanguage=ML.VB Then
Return "=new " & ColumnType
Else
Return "=new " & ColumnType & ";"
End If
End Select
End Function
'返回字段的对应数据类别
Public Function GetColumnDataType(ByVal MakeLang As ML, ByVal ColumnField As SchemaExplorer.ColumnSchema) As String
Select Case ColumnField.NativeType.ToLower
Case "int", "tinyint", "smallint", "bigint"
If MakeLang = ML.VB Then
Return "Integer"
ElseIf MakeLang = ML.CSharp Then
Return "int"
End If
Case "decimal", "float", "money", "numeric", "smallmoney"
If MakeLang = ML.VB Then
Return "Double"
ElseIf MakeLang = ML.CSharp Then
Return "double"
End If
Case "char", "nchar", "ntext", "text", "varchar", "nvarchar"
If MakeLang = ML.VB Then
Return "String"
ElseIf MakeLang = ML.CSharp Then
Return "string"
End If
Case "smalldatetime", "timestamp", "datetime"
Return "DateTime"
Case "bit"
If MakeLang = ML.VB Then
Return "Boolean"
ElseIf MakeLang = ML.CSharp Then
Return "bool"
End If
Case "binary", "image", "varbinary"
If MakeLang = ML.VB Then
Return "Byte()"
ElseIf MakeLang = ML.CSharp Then
Return "byte[]"
End If
End Select
End Function
</script>
生成后的代码以VB.NET示例:
Imports System.Data
Imports EagleFlyFCL.Data
Namespace ModelNamespace Model
<Serializable()>Public Class CategoriesEntityClass CategoriesEntity
Public Sub New()Sub New()
'ToDo
End Sub
Private _categoryID As Integer=0
<ColumnName()> Public Property CategoryID()Property CategoryID() As Integer
Get
Return _categoryID
End Get
Set(ByVal Value As Integer)
_categoryID = Value
End Set
End Property
Private _categoryName As String= ""
<ColumnName()> Public Property CategoryName()Property CategoryName() As String
Get
Return _categoryName
End Get
Set(ByVal Value As String)
_categoryName = Value
End Set
End Property
Private _description As String= ""
<ColumnName()> Public Property Description()Property Description() As String
Get
Return _description
End Get
Set(ByVal Value As String)
_description = Value
End Set
End Property
Private _picture As Byte()
<ColumnName()> Public Property Picture()Property Picture() As Byte()
Get
Return _picture
End Get
Set(ByVal Value As Byte())
_picture = Value
End Set
End Property
Private _startDate As DateTime=System.DateTime.Now
<ColumnName()> Public Property StartDate()Property StartDate() As DateTime
Get
Return _startDate
End Get
Set(ByVal Value As DateTime)
_startDate = Value
End Set
End Property
Private _amount As Integer=0
<ColumnName()> Public Property Amount()Property Amount() As Integer
Get
Return _amount
End Get
Set(ByVal Value As Integer)
_amount = Value
End Set
End Property
Private _isBuy As Boolean=False
<ColumnName()> Public Property IsBuy()Property IsBuy() As Boolean
Get
Return _isBuy
End Get
Set(ByVal Value As Boolean)
_isBuy = Value
End Set
End Property
Private _price As Double=0
<ColumnName()> Public Property Price()Property Price() As Double
Get
Return _price
End Get
Set(ByVal Value As Double)
_price = Value
End Set
End Property
End Class
End Namespace