By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,292 Members | 1,642 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,292 IT Pros & Developers. It's quick & easy.

Singleton Design Pattern & Object persistence

P: n/a
Hey guys, I've been hitting a brick wall with a problem I've come accross
in developing an application.

Background:
The application uses one primary class that I'm trying to implement with
the singleton pattern in mind. The design pattern seems to be working
properly (complex and simple variations of it) Serialzation/Deserialzation
seems to work okay.

I run into a problem when I attempt to load the file back into the
application. Deserialzation works fine and the object is restored, and I
print out it's properties using the object I deserialized it to.

Problem:
Problems occur when I try to use the deserialized object (contained &
referenced in the singleton class) from a different method. Printing the
object's properties reveals only a blank object (no property values
available -- other than defaults provided)

I'm quite tired atm, so I'm thinking it's something really stupid like the
scope of the object in the first method. Can anyone shed some light on this?
I feel like an idiot right now.
'---------------------------------------------------------------
'Deserialization code in ProcessProject() Method
'---------------------------------------------------------------

Dim fs As New FileStream(StrPathToProject, FileMode.Open, FileAccess.Read)
Dim bf As New BinaryFormatter

'CXiNKProject is the class/object I'm restoring
Dim ObjProject As CXiNKProject
'Creating a unique instance of the class
ObjProject = CXiNKSingleton.GetInstance()
'deserialize & cast to the object
ObjProject = DirectCast(bf.Deserialize(fs), CXiNKProject)

'cleanup
fs.Close()
bf = Nothing

'initialization code etc..."
'---------------------------------------------------------------
'---------------------------------------------------------------
'Singleton Class Code
'---------------------------------------------------------------
Imports System
Imports System.Threading

Public Class CXiNKSingleton
Implements IDisposable

#Region "Properties"
Private Shared __InstMutex As Mutex
Private Shared __InstanceCount As Integer = 0
Private Shared __SingletonProject As CXiNKProject ' = Nothing
Private Shared __IsActive As Boolean = False
#End Region

#Region "Get/Set Properties"
Public Shared Property IsActive() As Boolean
Get
Return __IsActive
End Get
Set(ByVal Value As Boolean)
__IsActive = Value
End Set
End Property

Private Shared Property InstMutex() As Mutex
Get
Return __InstMutex
End Get
Set(ByVal Value As Mutex)
__InstMutex = Value
End Set
End Property

Public Shared Property InstanceCount() As Integer
Get
Return __InstanceCount
End Get
Set(ByVal Value As Integer)
__InstanceCount = Value
End Set
End Property

Public Shared ReadOnly Property GetInstance() As CXiNKProject
Get
If __SingletonProject Is Nothing Then
InstMutex = New Mutex
InstMutex.WaitOne()
If __SingletonProject Is Nothing Then
Console.WriteLine("No existing instance of CXiNKProject
found...")
Console.WriteLine("Creating new instance of
CXiNKProject...")

__SingletonProject = New CXiNKProject
IsActive = True
InstanceCount += 1
InstMutex.ReleaseMutex()
End If
End If

Console.WriteLine("Returning new/existing instance of
CXiNKProject: " & InstanceCount())
Console.WriteLine()
Return __SingletonProject
End Get
End Property
#End Region

#Region "Constructor/Destructor"
Protected Sub New()
'override default constructor
End Sub

Public Sub Dispose() Implements System.IDisposable.Dispose
'reset everything
IsActive = False
InstanceCount = 0
__SingletonProject = Nothing
Console.WriteLine("Disposing of object: " & "IsActive " & IsActive()
& " InstanceCount: " & InstanceCount())
End Sub
#End Region
End Class
'---------------------------------------------------------------

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Problem is here...

ObjProject = CXiNKSingleton.GetInstance()
'deserialize & cast to the object
ObjProject = DirectCast(bf.Deserialize(fs), CXiNKProject)
You get a reference to the one and only singleton and store it in a
local variable. Then you deserialize a stream to the same local
object, overwriting the local reference to the singleton.

But then the function exists and the local reference is released.

What you need is a SetInstance() method or some other method
(preferrably internal to the singleton class) that sets the singleton
instance to be the deserialized object.

HTH,

Sam
B-Line is now hiring one VB.NET developer for WinForms + WebServices
position with strong possibility of ASPX in future. Seaking mid to
senior level developer. For information or to apply e-mail
sam_blinex_com.
Nov 21 '05 #2

P: n/a
Wicked! thanks, I'm going to update my classes to handle this problem. I
really appreciate the assitance!

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.