使用NVelocity实现代码生成

首先实现一个NVelocity的工具类:

Imports System.IO

Imports NVelocity
Imports NVelocity.App

Public Class Velocity

Private Shared _Templates As New Dictionary(Of String, Template)

Public Shared Sub RegisterTemplate(name As String, filePath As String)
If Not _Templates.ContainsKey(name) Then
_Templates.Add(name, Engine.GetTemplate(filePath))
End If
End Sub

Private Shared _Engine As VelocityEngine = Nothing
Public Shared ReadOnly Property Engine As VelocityEngine
Get
If _Engine Is Nothing Then
_Engine = New VelocityEngine()
_Engine.Init()
End If
Return _Engine
End Get
End Property

Public Shared Function Merge(templateName As String, parameters As IDictionary(Of String, Object)) As String
Dim _Context As New VelocityContext()
For Each key As String In parameters.Keys
_Context.Put(key, parameters.Item(key))
Next
Using sw As New StringWriter
_Templates.Item(templateName).Merge(_Context, sw)
Return sw.ToString()
End Using
End Function

End Class

这里的RegisterTemplate方法,通过名称/路径的方式将Velocity模板文件注册给工具类。而通过Merge方法即可根据传入的参数填写到模板中并返回结果。
参考NVelocity模板文件如下:

Imports System
Imports System.Runtime.Serialization
Imports System.Collections.Generic

Namespace ${Namespace}.Model

<Serializable()> _
<DataContract()> _
Public Partial Class ${Table.Name}DTO
Inherits ModelBase

#foreach ($Column in ${Table.Columns})
Private _${Column.Name} As ${Column.Type}
#end
#foreach ($Column in ${Table.Columns})

Public Property ${Column.Name} As ${Column.Type}
Get
Return _${Column.Name}
End Get
Set(value As ${Column.Type})
_${Column.Name} = value
End Set
End Property
#end

End Class
End Namespace

调用代码如下(其中Table和Column为描述表及列信息的数据实体类):

Velocity.RegisterTemplate("dto", "dto.vm")

Dim t As New Table("AccBatch")

t.Add(New Column With {.Name = "BATCHID", .SqlType = "int", .IsNullable = "0"})
t.Add(New Column With {.Name = "DATE", .SqlType = "datetime", .IsNullable = "1"})
t.Add(New Column With {.Name = "STARTTIME", .SqlType = "datetime", .IsNullable = "1"})
t.Add(New Column With {.Name = "ENDTIME", .SqlType = "datetime", .IsNullable = "1"})
t.Add(New Column With {.Name = "SUCCESSFLAG", .SqlType = "char", .IsNullable = "1"})
t.Add(New Column With {.Name = "UPDATETIME", .SqlType = "datetime", .IsNullable = "1"})
t.Add(New Column With {.Name = "EXECUTEFLAG", .SqlType = "char", .IsNullable = "1"})

Dim parameters As New Dictionary(Of String, Object)
parameters.Add("Namespace", "cn.com.dhc.Hiway")
parameters.Add("Table", t)

Using swVb As New StreamWriter(vb)
swVb.Write(Velocity.Merge("dto", parameters))
swVb.Flush()
End Using

通过如上方法即可生成代码如下(代码生成结果):

Imports System
Imports System.Runtime.Serialization
Imports System.Collections.Generic

Namespace cn.com.dhc.Hiway.Model

<Serializable()> _
<DataContract()> _
Public Partial Class AccBatchDTO
Inherits ModelBase

Private _BATCHID As Integer
Private _DATE As DateTime?
Private _STARTTIME As DateTime?
Private _ENDTIME As DateTime?
Private _SUCCESSFLAG As String
Private _UPDATETIME As DateTime?
Private _EXECUTEFLAG As String

Public Property BATCHID As Integer
Get
Return _BATCHID
End Get
Set(value As Integer)
_BATCHID = value
End Set
End Property

Public Property DATE As DateTime?
Get
Return _DATE
End Get
Set(value As DateTime?)
_DATE = value
End Set
End Property

Public Property STARTTIME As DateTime?
Get
Return _STARTTIME
End Get
Set(value As DateTime?)
_STARTTIME = value
End Set
End Property

Public Property ENDTIME As DateTime?
Get
Return _ENDTIME
End Get
Set(value As DateTime?)
_ENDTIME = value
End Set
End Property

Public Property SUCCESSFLAG As String
Get
Return _SUCCESSFLAG
End Get
Set(value As String)
_SUCCESSFLAG = value
End Set
End Property

Public Property UPDATETIME As DateTime?
Get
Return _UPDATETIME
End Get
Set(value As DateTime?)
_UPDATETIME = value
End Set
End Property

Public Property EXECUTEFLAG As String
Get
Return _EXECUTEFLAG
End Get
Set(value As String)
_EXECUTEFLAG = value
End Set
End Property

End Class
End Namespace

NVelocity是不错的模板引擎,支持变量、对象、对象方法、循环、判断等模板语法,强大的模板模型支持使得使用其进行模板开发能大大提高系统效率,值得使用。


posted on 2012-02-22 17:02  Richard.Tsui  阅读(487)  评论(0编辑  收藏  举报