Hi all. I'm a little curious as to how singletons work. In particular, I'm confused as to how they maintain state through multiple requests. If I have a Class named ClassA and it has a Shared method called getClassA, what is the difference between coding the getClassA method in the following two ways:
#1 - Using the application Cache
Friend Shared Function getClassA() As ClassA
If (Not HttpContext.Current.Cache.Item("objClassA"
Is Nothing) Then
Return HttpContext.Current.Cache.Item("objClassA"
Else
Dim objClassA As New ClassA
HttpContext.Current.Cache.Item("objClassA"
= objClassA
Return objClassA
End If
End Function
#2 - Singleton Pattern
Friend Shared Function getClassA() As ClassA
If isInstantiated = False Then
objClassA = New ClassA
isInstantiated = True
Return objClassA
Else
Return objClassA
End If
End Function
Regards.
Stephen.
#1 - Using the application Cache
Friend Shared Function getClassA() As ClassA
If (Not HttpContext.Current.Cache.Item("objClassA"
Return HttpContext.Current.Cache.Item("objClassA"
Else
Dim objClassA As New ClassA
HttpContext.Current.Cache.Item("objClassA"
Return objClassA
End If
End Function
#2 - Singleton Pattern
Friend Shared Function getClassA() As ClassA
If isInstantiated = False Then
objClassA = New ClassA
isInstantiated = True
Return objClassA
Else
Return objClassA
End If
End Function
Regards.
Stephen.