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


转载于:https://www.cnblogs.com/richardcktsui/archive/2012/02/22/2363426.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值