473,395 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Singleton class and dispose (question of style really)

Hi.

I don't have a problem per se, I was just wondering if anyone can
offer some opinions about the best way to go about creating and
disposing of a Singleton class.

I have a class (handling DB connections) which I have defined as a
singleton, using the following pattern (VB.net code)

Private Shared mInstance As clsConnection = Nothing

Public Shared ReadOnly Property Instance() As clsConnection
Get
If mInstance Is Nothing Then
mInstance = New clsConnection
End If
Instance = mInstance
End Get
End Property

I use this in many functions in my project, and whenever I do use it,
it's usually like this;

Private Sub MySub()
Dim myConn as clsConnection
try
myConn.Instance.SomeDBops()
catch ex as exception
finally
myConn.Instance.Dispose
end try
end sub

So far so good, I'm relatively pleased that the class will be
instanciated and disposed of at the correct times.

But what if I was to call MySub from another sub that also wished to
use a/the connection object.

Private Sub AnotherSub()
Dim myConn as clsConnection
try
myConn.Instance.SomeDBops()

MySub()
catch ex as exception
finally
myConn.Instance.Dispose
end try
end sub
The singleton will be disposed of within MySub, but then when
execution reaches the finally statement in AnotherSub, an instance of
clsConnection is created just so it can be disposed of. If have lots
of similarly nested calls this happens a lot. So what do I do about
this. I could always make my connection some global or member, but I
would really like resources to be freed as soon as they are no longer
required.

So I thought maybe I could define a singleton style dispose.
Something like this;

Public Shared Sub singleDispose()
If Not mInstance Is Nothing Then
mInstance.Dispose() 'Dispose sets mInstance to Nothing
End If
End Sub

So now in my finally clauses I call myConn.singleDispose, avoiding the
case where an instance is created only to be disposed of.

Can anyone offer an opinion as to whether this is a good thing to do.
Am I adding needless overhead here? Should I care about objects being
instanciated, only to be disposed? Should I except that using members
and/or globals would be a better approach? Vague I know but at the
moment I find this approach rather pleasing, but I'm not too sure if I
should. Have I misunderstood the usage of singletons and .net object
creatation, or is this a good way to go.

TIA for any comments.
Jul 21 '05 #1
2 2680
Simon <sj****@webpage-marketing.com> wrote:
I don't have a problem per se, I was just wondering if anyone can
offer some opinions about the best way to go about creating and
disposing of a Singleton class.
Creating, yes. Disposing - I have fewer opinions...
I have a class (handling DB connections) which I have defined as a
singleton, using the following pattern (VB.net code)

Private Shared mInstance As clsConnection = Nothing

Public Shared ReadOnly Property Instance() As clsConnection
Get
If mInstance Is Nothing Then
mInstance = New clsConnection
End If
Instance = mInstance
End Get
End Property
That isn't thread-safe.

See http://www.pobox.com/~skeet/csharp/singleton.html

<snip>
The singleton will be disposed of within MySub, but then when
execution reaches the finally statement in AnotherSub, an instance of
clsConnection is created just so it can be disposed of. If have lots
of similarly nested calls this happens a lot. So what do I do about
this. I could always make my connection some global or member, but I
would really like resources to be freed as soon as they are no longer
required.
It sounds like you don't *really* want a singleton, to be honest.
So I thought maybe I could define a singleton style dispose.
Something like this;

Public Shared Sub singleDispose()
If Not mInstance Is Nothing Then
mInstance.Dispose() 'Dispose sets mInstance to Nothing
End If
End Sub

So now in my finally clauses I call myConn.singleDispose, avoiding the
case where an instance is created only to be disposed of.


Well, it's no longer really a singleton. You could end up with two
different instances at the same time - someone could "fetch" one and
keep a reference to it, then another method disposes it, then a third
method uses the property again - the first class would still have a
reference to a now-disposed object.

Singletons are designed to be long-lasting. Objects implementing
IDisposable are generally designed to be transient - the two just don't
go well together.

Perhaps you should look at a factory pattern instead?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Thanks for the reply.

That many instances issues struck me as soon as I posted that message
(Isn't it always the way). Yes, I don't think the singleton is really
what I need (or my proposed perversion of it). Thanks for the
suggestion of the factory pattern, and the clarification of Singletons
and Dispose not really being compatable, I'll keep that in mind next
time I start to go anywhere near this line of reasoning.

Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
Simon <sj****@webpage-marketing.com> wrote:
I don't have a problem per se, I was just wondering if anyone can
offer some opinions about the best way to go about creating and
disposing of a Singleton class.


Creating, yes. Disposing - I have fewer opinions...
I have a class (handling DB connections) which I have defined as a
singleton, using the following pattern (VB.net code)

Private Shared mInstance As clsConnection = Nothing

Public Shared ReadOnly Property Instance() As clsConnection
Get
If mInstance Is Nothing Then
mInstance = New clsConnection
End If
Instance = mInstance
End Get
End Property


That isn't thread-safe.

See http://www.pobox.com/~skeet/csharp/singleton.html

<snip>
The singleton will be disposed of within MySub, but then when
execution reaches the finally statement in AnotherSub, an instance of
clsConnection is created just so it can be disposed of. If have lots
of similarly nested calls this happens a lot. So what do I do about
this. I could always make my connection some global or member, but I
would really like resources to be freed as soon as they are no longer
required.


It sounds like you don't *really* want a singleton, to be honest.
So I thought maybe I could define a singleton style dispose.
Something like this;

Public Shared Sub singleDispose()
If Not mInstance Is Nothing Then
mInstance.Dispose() 'Dispose sets mInstance to Nothing
End If
End Sub

So now in my finally clauses I call myConn.singleDispose, avoiding the
case where an instance is created only to be disposed of.


Well, it's no longer really a singleton. You could end up with two
different instances at the same time - someone could "fetch" one and
keep a reference to it, then another method disposes it, then a third
method uses the property again - the first class would still have a
reference to a now-disposed object.

Singletons are designed to be long-lasting. Objects implementing
IDisposable are generally designed to be transient - the two just don't
go well together.

Perhaps you should look at a factory pattern instead?

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rajarshi Guha | last post by:
Hi, I'm having a little problem with understanding the working of a singleton and borg class. Basically I nedd an class whose state will be shared across several modules. I found the stuff on the...
4
by: Mike | last post by:
Please help this is driving me nuts. I have 2 forms, 1 user class and I am trying to implement a singleton class. Form 1 should create a user object and populate some properties in user. Form2...
2
by: Dan | last post by:
All, I am developing an Object that wraps an old C library. Baeed upon how the object will be used I decided the best bet would be to make the object a singleton. Additionally, because the...
16
by: ed_p | last post by:
Hello, I have implemented the singleton pattern for a class to hold a SqlConnection object that will be used thruout the application to create commands. My application is a simple Windows Form...
2
by: Chris Murphy via DotNetMonster.com | last post by:
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...
2
by: Simon | last post by:
Hi. I don't have a problem per se, I was just wondering if anyone can offer some opinions about the best way to go about creating and disposing of a Singleton class. I have a class (handling...
12
by: Preets | last post by:
Can anyone explain to me the exact use of private constructors in c++ ?
7
by: ThunderMusic | last post by:
Hi, I have a problem regarding singletons in C#. What I would like to do is the following ClassA is a singleton ClassB inherits from ClassA and is also a Singleton there cannot and instance of...
2
by: Bob Johnson | last post by:
Just wondering the extent to which some of you are implementing classes as Singletons. I'm working on a brand new project and, early on, identified some obvious candidates. By "obvoius candidates"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.