问题描述
|
我有一个表Users,带有相关的子表UserSecurityGroups。在我的GUI中,操作员将从列表中选择一个用户。该程序将检索用户记录,并允许操作员以一种形式编辑用户数据。操作员还可以在另一个表单上编辑此用户的UserSecurityGroups。
我正在考虑使用单例类进行用户实例检索并将其持久化回数据库。如果这是一个好习惯,那么我想将其与数据库中的许多其他表一起使用。我的问题是:这是一种好习惯吗?我错过了什么陷阱?您会推荐什么替代方案?因为我也有包含三个或四个关系级别的表(与上面示例中的两个相对),您的建议会改变吗?
这是我建议的代码:
Imports System.Data.Linq
Public Class UserConduit
Implements IDisposable
Private Shared _thisUserConduit As UserConduit
Private Shared _thisContext As VulcanDataContext
Protected Sub New()
_thisContext = New VulcanDataContext
End Sub
Public Shared Function GetInstance()
If _thisUserConduit Is Nothing Then
_thisUserConduit = New UserConduit
End If
Return _thisUserConduit
End Function
Public Function GetUserByID(ByVal thisUserName As String) As User
Return _thisContext.Users.SingleOrDefault(Function(u) u.Username = thisUserName)
End Function
Public Function Save() As ChangeSet
Dim thisSet = _thisContext.GetChangeSet
Try
_thisContext.SubmitChanges()
Catch ex As Exception
Throw
End Try
Return thisSet
End Function
Public Function Save(ByVal thisUser As User) As ChangeSet
If thisUser.Modified Is Nothing Then
_thisContext.Users.InsertOnSubmit(thisUser)
End If
Return Save()
End Function
#Region \" IDisposable Support \"
Private disposedValue As Boolean = False \' To detect redundant calls
\' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
\' free other state (managed objects).
_thisContext.Dispose()
End If
\' free your own state (unmanaged objects).
\' set large fields to null.
End If
Me.disposedValue = True
End Sub
\' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
\' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)