E
Eric
I have a VB.net dll project with a class that is a singleton. I've been
using this in winform apps without any problems. I would like to use this
same dll in a web form project but my singleton will cause problems because
some sessions may need different values in the singleton. I want to change
my singleton to store a private hashtable with different instances. I guess
this is more like a factory pattern now but that doesn't matter My main
concern is about thread safty. The code below is what I have so far. Is
this the correct way to protect access to the inner hashtable? Thanks for
the comments or suggestions.
Eric
Friend NotInheritable Class RuleManager
Private Shared mInnerList As new HashTable
Private Sub New()
'hide constructor
End Sub
Public Shared ReadOnly Property Instance(Optional ByVal UniqueKey As
String = "") As RuleManager
Get
If mInnerList.Contains(UniqueKey) Then
Return DirectCast(mInnerList(UniqueKey), RuleManager)
End If
SyncLock mInnerList.SyncRoot
If Not mInnerList.Contains(UniqueKey) Then
mInnerList(UniqueKey) = New RuleManager
End If
Return DirectCast(mInnerList(UniqueKey), RuleManager)
End SyncLock
End Get
End Property
End class
using this in winform apps without any problems. I would like to use this
same dll in a web form project but my singleton will cause problems because
some sessions may need different values in the singleton. I want to change
my singleton to store a private hashtable with different instances. I guess
this is more like a factory pattern now but that doesn't matter My main
concern is about thread safty. The code below is what I have so far. Is
this the correct way to protect access to the inner hashtable? Thanks for
the comments or suggestions.
Eric
Friend NotInheritable Class RuleManager
Private Shared mInnerList As new HashTable
Private Sub New()
'hide constructor
End Sub
Public Shared ReadOnly Property Instance(Optional ByVal UniqueKey As
String = "") As RuleManager
Get
If mInnerList.Contains(UniqueKey) Then
Return DirectCast(mInnerList(UniqueKey), RuleManager)
End If
SyncLock mInnerList.SyncRoot
If Not mInnerList.Contains(UniqueKey) Then
mInnerList(UniqueKey) = New RuleManager
End If
Return DirectCast(mInnerList(UniqueKey), RuleManager)
End SyncLock
End Get
End Property
End class