Embarcadero ERStudio 是一个数据模型工具,用起来很方便。在ERStudio里建完数据模型后,可以利用宏来帮助生成对应的实体类文件以及对应的简单的数据访问类文件。为了实现这一目的,需要自己写脚本,支持的语言为 VB(以前从没写过VB程序,还好可以利用 Microsoft Visual Studio 文档),ERStudio 也提供了一系列的帮助文档,也有相关DEMO,最主要的是要获得相关Model的属性信息。这些可以在 ERStudio 文档里查看帮助。
在 ERStudio 自带的编辑工具来写VB脚本还不错,Tools -- 〉Basic Macro Editor 打开编辑器。然后就可以开始写脚本了,编辑器提供了一个简单的 窗体编辑工具 Edit UserDialog ,可以画出用户交互界面,选择后的控件如果不知道怎么用的话,将光标停在CODE上按F1会打开帮助文档,里面符有简单的Demo。
编辑器也提供Debug功能,不过感觉调试起来很痛苦,没有VS里调试爽,不过用处还是蛮大的,脚本写好后默认保存的路径在安装文件所属路径的 Macros 文件夹里,通过 Macro Shortcuts -- Add/Remove Macro Shortcuts 来管理宏文件。打开 Add/Remove Macro 对话框后可以在 Available Macros 下边看到可用的宏文件,在Selected Macros下面可以看到已经选择使用的宏文件。
点击这里可以下载示例脚本 .
下面的代码获取Model和其下的SubModel:
Dim entNames As Variant
Dim entCount As Variant
Dim varModel As Model
Dim varSubModel As SubModel
'Get Model Object
Set varModel =DiagramManager.ActiveDiagram.ActiveModel
'Get SubModel Object
Set varSubModel = varModel.ActiveSubModel
'Fill The EntityName To entName and Get the Entity Count
varSubModel.EntityNames(entNames,entCount)
获取SubModel的TableName:Dim entCount As Variant
Dim varModel As Model
Dim varSubModel As SubModel
'Get Model Object
Set varModel =DiagramManager.ActiveDiagram.ActiveModel
'Get SubModel Object
Set varSubModel = varModel.ActiveSubModel
'Fill The EntityName To entName and Get the Entity Count
varSubModel.EntityNames(entNames,entCount)
varEntity.TableName
获取SubModel的指定索引的字段'Get Entity Attribute
Function GetAttribute(attrs As Attributes, Index As Integer)
Dim attr As AttributeObj
For Each attr In attrs
If attr.SequenceNumber=Index Then
Set GetAttribute=attr
Exit Function
End If
Next
Set GetAttribute=Nothing
End Function
判断某一字段是不是主键:Function GetAttribute(attrs As Attributes, Index As Integer)
Dim attr As AttributeObj
For Each attr In attrs
If attr.SequenceNumber=Index Then
Set GetAttribute=attr
Exit Function
End If
Next
Set GetAttribute=Nothing
End Function
Set attr = GetAttribute(varEntity.Attributes, i)
If Not attr Is Nothing Then
If attr.PrimaryKey Then
'do something
End If
End If
还有一个很重要的一点就是要把数据库类型转化为.Net对应的类型:If Not attr Is Nothing Then
If attr.PrimaryKey Then
'do something
End If
End If
'Get .NET Data Type
Function DbTypeToNetType(attr As Object)
Dim netType As String
Select Case attr.Datatype
Case "CHAR"
netType = "string"
Case "VARCHAR2"
netType = "string"
Case "NVARCHAR"
netType = "string"
Case "NVARCHAR2"
netType = "string"
Case "VARCHAR"
netType = "string"
Case "NTEXT"
netType = "string"
Case "NTEXT/LONG NVARCHAR"
netType = "string"
Case "DATETIME"
netType = "DateTime"
Case "DATE"
netType = "DateTime"
Case "DECIMAL"
netType = "decimal"
Case "FLOAT"
netType = "float"
Case "NUMERIC"
If attr.DataScale = 0 Then
netType = IIf(attr.DataLength > 9,"decimal","int")
Else
netType = IIf(attr.DataScale < 29,"decimal","double")
End If
Case "INTEGER"
netType = "int"
Case "BIT"
netType = "bool"
Case "SMALLINT"
netType = "int"
Case Else
netType = "string"
End Select
DbTypeToNetType = netType
End Function
将字符串中的下划线去掉,将下划线后的第一个字母变为大写返回,如:_ab_cde_ 返回为 AbCde:Function DbTypeToNetType(attr As Object)
Dim netType As String
Select Case attr.Datatype
Case "CHAR"
netType = "string"
Case "VARCHAR2"
netType = "string"
Case "NVARCHAR"
netType = "string"
Case "NVARCHAR2"
netType = "string"
Case "VARCHAR"
netType = "string"
Case "NTEXT"
netType = "string"
Case "NTEXT/LONG NVARCHAR"
netType = "string"
Case "DATETIME"
netType = "DateTime"
Case "DATE"
netType = "DateTime"
Case "DECIMAL"
netType = "decimal"
Case "FLOAT"
netType = "float"
Case "NUMERIC"
If attr.DataScale = 0 Then
netType = IIf(attr.DataLength > 9,"decimal","int")
Else
netType = IIf(attr.DataScale < 29,"decimal","double")
End If
Case "INTEGER"
netType = "int"
Case "BIT"
netType = "bool"
Case "SMALLINT"
netType = "int"
Case Else
netType = "string"
End Select
DbTypeToNetType = netType
End Function
'Remove UnderLine From String And Change The First Character To Capital
Function RemoveUnderLine(varRetStr As String, varString As String,length As Integer )
If length > 0 Then
Dim char As String
char = Left$(varString,1)
If char <> "_" Then
varRetStr = IIf(Len(varRetStr) = 0,UCase(char),varRetStr & char)
varString = Right$(varString,length-1)
Else
varString = Right$(varString,length-1)
If Len(varString) > 0 Then
varString = UCase(Left$(varString,1)) & Right$(varString,Len(varString)-1)
End If
End If
varRetStr = RemoveUnderLine (varRetStr,varString,Len(varString))
End If
RemoveUnderLine = varRetStr
End Function
这样会帮我们省去大量的工作,可订制性也比较好,当然不要把这个和别的代码生成工具比较....Function RemoveUnderLine(varRetStr As String, varString As String,length As Integer )
If length > 0 Then
Dim char As String
char = Left$(varString,1)
If char <> "_" Then
varRetStr = IIf(Len(varRetStr) = 0,UCase(char),varRetStr & char)
varString = Right$(varString,length-1)
Else
varString = Right$(varString,length-1)
If Len(varString) > 0 Then
varString = UCase(Left$(varString,1)) & Right$(varString,Len(varString)-1)
End If
End If
varRetStr = RemoveUnderLine (varRetStr,varString,Len(varString))
End If
RemoveUnderLine = varRetStr
End Function
提高工作效率才是硬道理~~~~~~