OrFlying VB.net版代码示例

OrFlying VB.Net 的开发主要考虑了vb.net 对面向对象的支持,所以抽象出了基类,同时增加了一些方法。另外考虑到对象层的运行效率,加入了缓冲层的支持。

命名:CClsXXXX 为基本类 CColXXXX 为集合类 CEngXXXX为引擎类

1 基类层示例

Public Class CClsParent

'实体对象是否为新的实体对象
Private mBIsNew As Boolean
'实体对象是否已经被删除
Private mBIsDel As Boolean
'实体对象是否被更改
Private mBIsDirty As Boolean
'实体对象属性赋值的时候是否需要进行数据校验
Private mBIsCheck As Boolean

 

Public Property IsNew() As Boolean

Get
Return mBIsNew
End Get

Set(ByVal Value As Boolean)
mBIsNew = Value
End Set
End Property

Public Property IsDel() As Boolean

Get
Return mBIsDel
End Get

Set(ByVal Value As Boolean)
mBIsDel = Value
End Set

End Property

Public Property IsDirty() As Boolean
Get
Return mBIsDirty
End Get

Set(ByVal Value As Boolean)
mBIsDirty = Value
End Set
End Property

Public Property IsCheck() As Boolean

Get
Return mBIsCheck
End Get

Set(ByVal Value As Boolean)
mBIsCheck = Value
End Set
End Property


End Class

Public Class CColParent
Implements System.Collections.IEnumerable

'集合的内部变量
Private mColObject As Collection
'表示该集合实例的内容是否为变化
Private mBIsChange As Boolean
'获得集合的元素的数目
Public ReadOnly Property Count() As Long
Get
Count = mColObject.Count
End Get
End Property

'设置或者返回系统是否发生变化的标志位
Public Property IsChange() As Boolean
Get
Return mBIsChange
End Get
Set(ByVal Value As Boolean)
mBIsChange = Value
End Set
End Property
'默认返回只读属性,返回指定Index的Item
Default Public ReadOnly Property Item(ByVal iObjIndexKey As Object) As Object
Get
Item = mColObject(iObjIndexKey)
End Get
End Property

'往集合里面添加一个Item
Public Function Add(ByVal iObjItem As Object, Optional ByVal iStrKey As String = "") As Boolean

Add = False
'添加元素
Try
'如果输入了关键字,加入关键字
If iStrKey = "" Then
mColObject.Add(iObjItem)
Else
mColObject.Add(iObjItem, iStrKey)
End If
Add = True
Catch ex As Exception
CGlbSharedMethods.AddSysErrMsg("CColParent.Add发生错误:" & ex.ToString, "集合类CDeptS添加错误,请联系关系员!")
Add = False
End Try

End Function
'根据关键字获得指定的Item
Public Function Find(ByRef oObjItem As Object, ByVal iObjItemKey As Object) As Boolean

Find = False
Try
oObjItem = mColObject(iObjItemKey)
Find = True
Catch ex As Exception
If ex.GetType.FullName = "System.ArgumentException" Then

Else
CGlbSharedMethods.AddSysErrMsg("CColParent:Find:" & ex.ToString, "返回数据发生错误!请联系管理员")

End If

End Try

End Function
'从集合中移除指定的Item
Public Function Remove(ByVal iObjIndexKey As Object) As Boolean

Try
mColObject.Remove(iObjIndexKey)
Remove = True
Catch ex As Exception
CGlbSharedMethods.AddSysErrMsg("CColParent.Remove发生错误:" & ex.ToString, "集合类CDeptS添加错误,请联系关系员!")
Remove = False
End Try

End Function

Public Function FindByProp(ByRef oObjItem As Object, ByVal iStrPropName As String, ByVal iPropValue As Object) As Boolean
FindByProp = False

Dim xItem As Object
For Each xItem In mColObject
If CallByName(xItem, iStrPropName, CallType.Get) = iPropValue Then
oObjItem = xItem
Return True
End If
Next
Return False
End Function
'清空对象
Public Function Clear() As Boolean

If mColObject Is Nothing Then Return True
Dim xObject As Object

For Each xObject In mColObject

xObject = Nothing
Next
'清空集合
mColObject = Nothing
'重新创建集合
mColObject = New Collection

Clear = True

End Function

'本属性允许用 For...Each 语法枚举该集合。
Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
'取消注释下行和更改下行以返回集合枚举数。
GetEnumerator = mColObject.GetEnumerator
End Function

'集合类初始化
Public Sub New()
MyBase.new()
mColObject = New Collection
End Sub
'集合类结束
Protected Overrides Sub Finalize()
mColObject = Nothing
MyBase.Finalize()

End Sub
End Class

2 缓冲管理类示例

'缓冲对象声明类

Module MdlEccCache

'ClsDept对象的缓冲容器,只要读一次,就自动加入,永久保存,直到最后退出。
Public gColDept As CColDept

End Module

'缓冲对象管理类

Public Class CCacheManage

'释放所有缓冲集合
Public Shared Function ReleaseAllCache() As Boolean
ReleaseAllCache = False
'释放Dept的缓冲对象
If Not (gColDept Is Nothing) Then ReleaseObjCache(gColDept)

'<%加入新的释放语句%>
ReleaseAllCache = True
End Function
'通用释放函数
Private Shared Sub ReleaseObjCache(ByRef ioCol As Object)

Dim xObject As Object
For Each xObject In ioCol
xObject = Nothing
Next
ioCol = Nothing

End Sub
'获得Dept对象的缓冲集合
Public Shared Function GetDeptCache(ByRef oColDept As CColDept) As Boolean
GetDeptCache = False

If gColDept Is Nothing Then gColDept = New CColDept()
oColDept = gColDept

GetDeptCache = True
End Function

End Class

3 业务类示例

Imports AgileSoft.CGlbDll

Public Class CClsDept
'继承实体基本类
Inherits CClsParent

Private strDeptName As String
Private strDeptDesc As String

Public Property DeptDesc() As String
Get
Return strDeptDesc
End Get
Set(ByVal Value As String)
strDeptDesc = Value
End Set
End Property


Public Property DeptName() As String
Get
Return strDeptName
End Get

Set(ByVal Value As String)
strDeptName = Value
End Set
End Property

 

End Class

Imports AgileSoft.CGlbDll

Public Class CColDept
'继承实体集合类
Inherits CColParent

'默认返回只读属性,返回指定Index的Item
Default Public Shadows ReadOnly Property Item(ByVal iObjIndexKey As Object) As CClsDept
Get
Item = MyBase.Item(iObjIndexKey)
End Get
End Property

'往集合里面添加一个Item
Public Shadows Function Add(ByVal iObjItem As CClsDept, Optional ByVal iStrKey As String = "") As Boolean

Return MyBase.Add(iObjItem, iStrKey)

End Function

Public Shadows Function Find(ByRef oClsDept As CClsDept, ByVal iStrDeptName As String) As Boolean
Return MyBase.Find(oClsDept, iStrDeptName)
End Function
End Class


'对象Dept的数据对象引擎类
Imports AgileSoft.CGlbDll
Imports System.Data.SqlClient

Public Class CEngDept


'************************************************************************
'方法说明:根据关键字返回对应的对象实例
'参数1: oClsDept为引用传递,返回的对象实例
'参数2: iStrDeptName为传入的对象的关键字
'参数3: oBFindObj表示对象是否成功获得
'************************************************************************
Public Function GetClsDeptByKey(ByRef oClsDept As CClsDept, ByVal iStrDeptName As String, Optional ByVal oBFindObj As Boolean = False) As Boolean

GetClsDeptByKey = False
Dim xStrSQL As String
'用作初始化对象的DataReader对象
Dim xDbReader As IDataReader

xStrSQL = "Select * from TestDept "
'加入外键条件
If iStrDeptName <> "" Then
xStrSQL = xStrSQL & " where DeptName='" & iStrDeptName & "'"
Else
CGlbSharedMethods.AddSysTipMsg("CEngDept:GetClsDeptByKey:关键字不能为空!", "")
End If
'首先检查缓冲区,如果已经存在直接返回,如果不存在,创建,并且加入缓冲区
Dim xColDept As CColDept
'返回缓冲集合
If CCacheManage.GetDeptCache(xColDept) Then
'如果对象已经存在,直接返回
If xColDept.Find(oClsDept, iStrDeptName) Then
oBFindObj = True
GoTo RightExit
End If
Else
CGlbSharedMethods.AddSysTipMsg("CEngDept:GetClsDeptByKey:无法返回缓冲集!", "")
End If

'缓冲区不存在,打开数据库实例实体对象,同时加入缓冲区
'返回属性,返回对象
If oClsDept Is Nothing Then oClsDept = New CClsDept()

'返回DataReader
If CGlbSharedMethods.GetReaderBySQL(xDbReader, xStrSQL) Then

If xDbReader.Read() Then

'实例化对象
oClsDept.DeptName = xDbReader("DeptName")
oClsDept.DeptDesc = xDbReader("DeptDesc")
xDbReader.Close()

'加入缓冲集
If Not (xColDept Is Nothing) Then
If Not xColDept.Add(oClsDept, oClsDept.DeptName) Then
CGlbSharedMethods.AddSysTipMsg("CEngDept:GetClsDeptByKey:无法将Dept对象加入缓冲集!", "")
End If
End If
'找到了数据,设置参数
oBFindObj = True
GoTo RightExit
Else
CGlbSharedMethods.AddSysTipMsg("CEngDept:GetClsDeptByKey:找不到指定的Dept对象!", "找不到相关部门!")
xDbReader.Close()

GoTo RightExit
End If

Else
'没有成功返回结果集
CGlbSharedMethods.AddSysErrMsg("CEngDept:GetClsDeptByKey:CallError CGlbSharedMethods.GetReaderBySQL", "")
GoTo CleanExit
End If

RightExit:
GetClsDeptByKey = True
CleanExit:
xDbReader = Nothing
xColDept = Nothing
End Function

'************************************************************************
'方法说明:根据条件返回对象实例的集合
'参数1: oColDept为引用传递,返回的对象的集合实例
'参数2: iStrSqlCondition为传入的获得对象的条件,为SQL格式
'参数3: oBFindObj表示对象的集合是否获得
'************************************************************************
Public Function GetColDeptBySQL(ByRef oColDept As CColDept, Optional ByVal iStrSqlCondition As String = "", Optional ByVal oBFindObj As Boolean = False) As Boolean

GetColDeptBySQL = False

Dim xStrSQL As String
Dim xClsDept As CClsDept
Dim xColDept As CColDept
Dim xBGetCache As Boolean
'用作初始化对象的DataReader对象
Dim xDbReader As IDataReader

xStrSQL = "Select * from TestDept "
'如果有额外条件,加入条件
If iStrSqlCondition <> "" Then
xStrSQL = xStrSQL & " where " & iStrSqlCondition
End If

'首先检查缓冲区,如果已经存在直接返回,如果不存在,创建,并且加入缓冲区
If CCacheManage.GetDeptCache(xColDept) Then
xBGetCache = True
Else
CGlbSharedMethods.AddSysTipMsg("CEngDept:GetColDeptBySQL:无法返回缓冲集!", "")
xBGetCache = False
End If

'返回属性,返回对象
If oColDept Is Nothing Then oColDept = New CColDept()


If CGlbSharedMethods.GetReaderBySQL(xDbReader, xStrSQL) Then

If xDbReader.Read() Then
'通过循环,把数据写入集合
Do
'如果缓冲集合中对象已经存在,直接返回。如果对象不存在,从数据库中实例化
If Not xBGetCache Or (xBGetCache And (Not xColDept.Find(xClsDept, xDbReader("DeptName")))) Then
'返回属性,返回对象
xClsDept = New CClsDept()
xClsDept.DeptName = xDbReader("DeptName")
xClsDept.DeptDesc = IIf(xDbReader("DeptDesc") Is DBNull.Value, "", xDbReader("DeptDesc"))

'加入缓冲区
If xBGetCache Then
If Not xColDept.Add(xClsDept, xClsDept.DeptName) Then
CGlbSharedMethods.AddSysTipMsg("CEngDept:GetClsDeptByKey:无法将Dept对象加入缓冲集!", "")
End If
End If
End If

'加入集合
If Not oColDept.Add(xClsDept, xClsDept.DeptName) Then
CGlbSharedMethods.AddSysErrMsg("CEngDept:GetColDeptBySQL:CallError CColDept.Add()", "")
xDbReader.Close()
GoTo CleanExit
End If
'清空对象
xClsDept = Nothing
oBFindObj = True

Loop Until Not xDbReader.Read
xDbReader.Close()
GoTo RightExit

Else
'查询结果中没有纪录
CGlbSharedMethods.AddSysTipMsg("CEngDept:GetColDeptBySQL:找不到指定的Dept对象!", "找不到相关部门!")
GoTo RightExit

End If

Else
'没有成功返回结果集
CGlbSharedMethods.AddSysErrMsg("CEngDept:GetColDeptBySQL:CallError CGlbSharedMethods.GetReaderBySQL", "")
GoTo CleanExit
End If

RightExit:
GetColDeptBySQL = True
CleanExit:
xClsDept = Nothing
xDbReader = Nothing

End Function
'更新一个对象的属性到数据库中
Public Function UpdateClsDept(ByRef iClsDept As CClsDept, Optional ByVal iBUpdateSubObj As Boolean = False) As Boolean
UpdateClsDept = False
'用来更新数据的ADO.Net数据对象
Dim xDataSet As DataSet
'命令对象,这里的SQL语句可以考虑使用属性代替
Dim xStrSQL As String
xStrSQL = "Select * from TestDept where DeptName='" & iClsDept.DeptName & "'"

Dim xTable As DataTable
Dim xRow As DataRow
'用来设置xTable的主键的DataColum数组
Dim xKey(1) As DataColumn
'返回缓冲区的引用
Dim xColDept As CColDept
Dim xBGetCache As Boolean
'首先检查缓冲区,如果已经存在直接返回,如果不存在,创建,并且加入缓冲区
If CCacheManage.GetDeptCache(xColDept) Then
xBGetCache = True
Else
CGlbSharedMethods.AddSysTipMsg("CEngDept:UpdateClsDept:无法返回缓冲集!", "")
xBGetCache = False
End If

'首先返回数据集合
If Not CGlbSharedMethods.GetDataSetBySQL(xDataSet, xStrSQL, "TestDept") Then
CGlbSharedMethods.AddSysErrMsg("CEngDept:UpdateClsDept:CallError CGlbSharedMethods.GetDataSetBySQL", "")
GoTo CleanExit
End If
If iClsDept.IsDel Or iClsDept.IsDirty Then
'如果没有纪录,报错退出
If xDataSet.Tables("TestDept").Rows.Count = 0 Then
CGlbSharedMethods.AddSysErrMsg("CEngDept:UpdateClsDept:结果集找不到数据,无法更新该对象!", "更新数据错误,请联系管理员!")
GoTo CleanExit
End If
End If
'更改数据
Try
xTable = xDataSet.Tables("TestDept")
If iClsDept.IsDel Then
xTable.Rows(0).Delete()
'从缓冲集合中移除
If xBGetCache Then
xColDept.Remove(iClsDept.DeptName)
End If
Else
If iClsDept.IsNew Or iClsDept.IsDirty Then
'根据IsNew和IsDirty确定xRow
If iClsDept.IsNew Then
xRow = xTable.NewRow
xTable.Rows.Add(xRow)
Else
'获得要更改的纪录
xRow = xTable.Rows(0)
End If
'更改纪录
xRow.BeginEdit()
'主键只有在增加的时候才能够被赋值
If iClsDept.IsNew Then xRow("DeptName") = iClsDept.DeptName
'增加其余字段信息
xRow("DeptDesc") = iClsDept.DeptDesc
xRow.EndEdit()

'如果返回缓冲集,加入缓冲集合
If xBGetCache Then
If Not xColDept.Add(iClsDept) Then
CGlbSharedMethods.AddSysTipMsg("CEngDept:UpdateClsDept:无法将Dept对象加入缓冲集!", "")
End If

End If
End If
End If

iClsDept.IsDel = False
iClsDept.IsDirty = False
iClsDept.IsNew = False

Catch ex As Exception
CGlbSharedMethods.AddSysErrMsg("CEngDept:UpdateClsDept:" & ex.ToString, "更新数据出现错误,请联系管理员!")
GoTo CleanExit
End Try

'更新数据到数据库中去
If Not CGlbSharedMethods.UpdateDataSet(xDataSet, "TestDept") Then
CGlbSharedMethods.AddSysErrMsg("CEngDept:UpdateClsDept:CallError CGlbSharedMethods.UpdateDataSet", "")
GoTo CleanExit
End If

RightExit:
UpdateClsDept = True
CleanExit:
xRow = Nothing
xTable = Nothing
xDataSet = Nothing
End Function
'更新一个集合中的全部的对象到数据库中
Public Function UpdateColDept(ByRef iColDept As CColDept, Optional ByVal iBUpdateSubObj As Boolean = False) As Boolean
UpdateColDept = False

Dim xDept As CClsDept
'用来更新数据的ADO.Net数据对象
Dim xDataSet As DataSet
'命令对象,这里的SQL语句可以考虑使用属性代替
Dim xStrSQL As String
xStrSQL = "Select * from TestDept"

Dim xTable As DataTable
Dim xRow As DataRow
'用来设置xTable的主键的DataColum数组
Dim xKey(1) As DataColumn

'返回缓冲区的引用
Dim xColDept As CColDept
Dim xBGetCache As Boolean
'首先检查缓冲区,如果已经存在直接返回,如果不存在,创建,并且加入缓冲区
If CCacheManage.GetDeptCache(xColDept) Then
xBGetCache = True
Else
CGlbSharedMethods.AddSysTipMsg("CEngDept:UpdateClsDept:无法返回缓冲集!", "")
xBGetCache = False
End If
'首先返回xCmd对应的数据集合
If Not CGlbSharedMethods.GetDataSetBySQL(xDataSet, xStrSQL, "TestDept") Then
CGlbSharedMethods.AddSysErrMsg("CEngDept:UpdateColDept:CallError CGlbSharedMethods.GetDataSetBySQL", "")
GoTo CleanExit
End If
'如果没有纪录,报错退出
If xDataSet.Tables("TestDept").Rows.Count = 0 Then
CGlbSharedMethods.AddSysErrMsg("CEngDept:UpdateColDept:结果集找不到数据,无法更新该对象!", "更新数据错误,请联系管理员!")
GoTo CleanExit
End If
'返回表,并且设置主键
xTable = xDataSet.Tables("TestDept")
xKey(0) = xTable.Columns("DeptName")
xTable.PrimaryKey = xKey

For Each xDept In iColDept

Try
xTable = xDataSet.Tables("TestDept")
If xDept.IsDel Then
xTable.Rows(0).Delete()
'从缓冲集合中移除
If xBGetCache Then
xColDept.Remove(xDept.DeptName)
End If
Else
If xDept.IsNew Or xDept.IsDirty Then
'根据IsNew和IsDirty确定xRow
If xDept.IsNew Then
xRow = xTable.NewRow
xTable.Rows.Add(xRow)
Else
'获得要更改的纪录
xRow = xTable.Rows(0)
End If
'更改纪录
xRow.BeginEdit()
'主键只有在增加的时候才能够被赋值
If xDept.IsNew Then xRow("DeptName") = xDept.DeptName
'增加其余字段信息
xRow("DeptDesc") = xDept.DeptDesc
xRow.EndEdit()

'如果返回缓冲集,加入缓冲集合
If xBGetCache Then
If Not xColDept.Add(xDept) Then
CGlbSharedMethods.AddSysTipMsg("CEngDept:UpdateColDept:无法将Dept对象加入缓冲集!", "")
End If

End If
End If
End If

xDept.IsDel = False
xDept.IsDirty = False
xDept.IsNew = False

Catch ex As Exception
CGlbSharedMethods.AddSysErrMsg("CEngDept:UpdateColDept:" & ex.ToString, "更新数据出现错误,请联系管理员!")
GoTo CleanExit
End Try
Next
'更新数据到数据库中去
If Not CGlbSharedMethods.UpdateDataSet(xDataSet, "TestDept") Then
CGlbSharedMethods.AddSysErrMsg("CEngDept:UpdateColDept:CallError CGlbSharedMethods.UpdateDataSet", "")
GoTo CleanExit
End If

RightExit:
UpdateColDept = True
CleanExit:
xRow = Nothing
xTable = Nothing
xDataSet = Nothing
End Function

End Class

 

业务类和缓冲管理类的代码全部由代码生成器自动产生

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值