473,326 Members | 2,588 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,326 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 2676
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.