473,385 Members | 1,478 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,385 software developers and data experts.

Generic Singleton?

Hi all - I've taken some code from MSDN and made a Generic singleton, so that
I don't have to write this code in many places:

public class Singleton<TEntity> where TEntity : class, new()

{
private static volatile TEntity instance = default(TEntity);
private static object syncRoot = new Object();

public static TEntity Instance
{
get
{
if (instance == default(TEntity))
{
lock (syncRoot)
{
if (instance == default(TEntity))
{
//instance = new TEntity();
}
}
}
return instance;
}
}
}

You would use it like this:

public class MyClass : Singleton<MyClass>
....

This works great - except that MyClass must have a public parameterless
constructor. Is there any way of getting around this? I have yet to find an
implementation that allows me to do what I want.

Thanks,Phil

Nov 23 '05 #1
7 2695
PMarino <PM*****@discussions.microsoft.com> wrote:
Hi all - I've taken some code from MSDN and made a Generic singleton, so that
I don't have to write this code in many places:


For a start, I wouldn't use that implementation. There's no point in
using double-checked locking - it's the kind of thing people try to do
to be clever, but if you're not careful it ends up not being threadsafe
:(

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

I would try to avoid having too many singletons in the first place
though - they're often (though far from always) a bad smell in design
terms. I'd keep each implementation separate.

That said, why can't you use a parameterless constructor? If you're
only going to have one of them, I can't see why you'd need any
variation between them - after all, the Instance property itself can't
specify any parameters.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 23 '05 #2
Hi Jon - thanks for your reply.
I'll have a look at that link.

I want to avoid making the constructors public so that client code cannot
create additional instances of the object.

Here's more detail: For my middle tier, I'm writing business objects.
Since they are stateless, they can be singletons - I'm trying to reduce the
number of objects that actually get created. There's actually no major
negative effect from creating these objects, though. And I'm making use of
generics in order to reduce code.

But I would like to keep things consistent, and the idea of a singleton is
to only have one (of course), so I'm trying to make it illegal to construct
one outside of the singleton.
"Jon Skeet [C# MVP]" wrote:
PMarino <PM*****@discussions.microsoft.com> wrote:
Hi all - I've taken some code from MSDN and made a Generic singleton, so that
I don't have to write this code in many places:


For a start, I wouldn't use that implementation. There's no point in
using double-checked locking - it's the kind of thing people try to do
to be clever, but if you're not careful it ends up not being threadsafe
:(

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

I would try to avoid having too many singletons in the first place
though - they're often (though far from always) a bad smell in design
terms. I'd keep each implementation separate.

That said, why can't you use a parameterless constructor? If you're
only going to have one of them, I can't see why you'd need any
variation between them - after all, the Instance property itself can't
specify any parameters.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 23 '05 #3
PMarino <PM*****@discussions.microsoft.com> wrote:
Hi Jon - thanks for your reply.
I'll have a look at that link.

I want to avoid making the constructors public so that client code cannot
create additional instances of the object.
Ah - so it's not that you want to use a constructor with a parameter,
it's that you want to use an internal parameterless constructor. That
makes sense.

You *could* do that with reflection - you'd lose compile-time type
safety, of course, but unit tests could certainly help there.
Here's more detail: For my middle tier, I'm writing business objects.
Since they are stateless, they can be singletons - I'm trying to reduce the
number of objects that actually get created. There's actually no major
negative effect from creating these objects, though. And I'm making use of
generics in order to reduce code.

But I would like to keep things consistent, and the idea of a singleton is
to only have one (of course), so I'm trying to make it illegal to construct
one outside of the singleton.


Have you considered using inversion of control for wiring things up,
instead of embedding the knowledge of singletons all over the place?
I've been using IoC in a recent Java project with great success - it's
been *fantastically* helpful when it comes to unit tests, as I can tell
one object to use a specific interface implementation of a collaborator
for unit tests (that implementation happening to be a mock) and then in
the "real thing" using Spring to wire it up to a real implementation.

I haven't used the .NET version of Spring, but I would imagine it's
just as helpful as the Java version :)

See http://www.springframework.net

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 23 '05 #4
Ok, so after looking at Jon's link below, I've modified my code. The goal is
to permit the constructor to only be called ONCE, and from within the
Instance method.
There is one implementation listed in the link that would allow this very
simply - but it requires a lock() on every Instance call.

BTW: Jon, that is a VERY helpful link!

Anyway, here's the code:
public class Singleton<TEntity> where TEntity : class, new()
{
private static int numConstructorCalls = 0;

private static bool inInstance = false;
private static object syncRoot = new Object();

public Singleton()
{
int constructorCalls = Interlocked.Add(ref numConstructorCalls,
0);

//
// If the constructor has been called more than once, new() must
// have been called.
//
if (constructorCalls > 1)
{
throw new Exception("Don't call new() on Singleton");
}

//
// Check to see if this is happening from Instance
//
lock (syncRoot)
{
if (inInstance == false)
{
throw new Exception("Don't call new() on Singleton");
}
Interlocked.Increment(ref numConstructorCalls);
}
}

public static TEntity Instance
{
get
{
TEntity theEntity = null;

int isConstructed = Interlocked.Add(ref numConstructorCalls,
0);
if (isConstructed == 0)
{
lock (syncRoot)
{
inInstance = true;
theEntity = Nested.instance;
inInstance = false;
}
}
else
{
theEntity = Nested.instance;
}
return theEntity;
}
}
private static class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested()
{
}

internal static readonly TEntity instance = new TEntity();
}
}

So the construction of the singleton is thread-safe, given the
implementation of the static variable.

Within the constructor, if the constructor has been called more than once,
and exception will be thrown. That's more of a shortcut, and doesn't need to
be there, but I thought it was a nice little performance improvement.

Then the lock is acquired, and the variable inInstance is checked. An
exception is thrown if the call is not from within the Instance method. I
don't mind doing this in the constructor, because it should only be called
once anyway.

In the Instance method, the number of constructor calls is checked. If this
is greater than one, then the object should already be constructed, and
Nested.instance is returned. If it is 0, then grab the lock and set
inInstance to true so that the first call to the constructor will not throw
an exception.

I've run multi-threaded tests and this seems to work. However, that doesn't
mean it's foolproof. I've spent a lot of time looking at this. I've
actually modified it a couple times after coming up with things that would
fail, but I can't come up with any more. Could anyone else tell me where I
might have made a mistake?
Thanks,
Phil
Nov 23 '05 #5
PMarino <PM*****@discussions.microsoft.com> wrote:
Ok, so after looking at Jon's link below, I've modified my code. The goal is
to permit the constructor to only be called ONCE, and from within the
Instance method.
You're not calling the constructor of Singleton though. You're calling
the constructor of TEntity - and nothing is making sure that TEntity's
constructor isn't being called elsewhere as well.
There is one implementation listed in the link that would allow this very
simply - but it requires a lock() on every Instance call.
Don't be *too* worried about locking a lot - obviously it's good from a
simplicity point of view if you can avoid it, but uncontested locks are
really, really cheap.

In this case, taking out a lock each time is definitely simpler in
terms of the threading code than using interlocked in various places.
BTW: Jon, that is a VERY helpful link!
Thanks :)
Anyway, here's the code:


You seem to be ensuring it's only constructed once in three different
ways. Why not just pick one of them - preferrably the simplest?

I'd suggest just going for:

public static class Singleton<TEntity> where TEntity : class, new()
{
static TEntity instance = new TEntity();

public static TEntity Instance
{
get
{
return instance;
}
}
}

Much simpler, and just as good, I believe. It doesn't load *quite* as
lazily, but I'm not sure of a good way round that - I'm not sure
whether your nested static constructor is actually doing what you want
it to or not. Do you definitely need non-beforefieldinit behaviour?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 23 '05 #6
Hi John - you're right, I'm not making sure that TEntity's constructor is not
being called. In the cases where I'm using it, though, it looks like this:

public class MyClass : Singleton<MyClass>
{
public MyClass() {}
}

I think I need to add a constraint to the Singleton class.
The problem is that I don't want someone to do this:

MyClass foo = new MyClass();

Since the Singleton requires a public constructor, this is actually allowed.
The simple implementation below would not prevent this, would it?

"Jon Skeet [C# MVP]" wrote:
PMarino <PM*****@discussions.microsoft.com> wrote:
Ok, so after looking at Jon's link below, I've modified my code. The goal is
to permit the constructor to only be called ONCE, and from within the
Instance method.


You're not calling the constructor of Singleton though. You're calling
the constructor of TEntity - and nothing is making sure that TEntity's
constructor isn't being called elsewhere as well.
There is one implementation listed in the link that would allow this very
simply - but it requires a lock() on every Instance call.


Don't be *too* worried about locking a lot - obviously it's good from a
simplicity point of view if you can avoid it, but uncontested locks are
really, really cheap.

In this case, taking out a lock each time is definitely simpler in
terms of the threading code than using interlocked in various places.
BTW: Jon, that is a VERY helpful link!


Thanks :)
Anyway, here's the code:


You seem to be ensuring it's only constructed once in three different
ways. Why not just pick one of them - preferrably the simplest?

I'd suggest just going for:

public static class Singleton<TEntity> where TEntity : class, new()
{
static TEntity instance = new TEntity();

public static TEntity Instance
{
get
{
return instance;
}
}
}

Much simpler, and just as good, I believe. It doesn't load *quite* as
lazily, but I'm not sure of a good way round that - I'm not sure
whether your nested static constructor is actually doing what you want
it to or not. Do you definitely need non-beforefieldinit behaviour?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 24 '05 #7
PMarino <PM*****@discussions.microsoft.com> wrote:
Hi John - you're right, I'm not making sure that TEntity's constructor is not
being called. In the cases where I'm using it, though, it looks like this:

public class MyClass : Singleton<MyClass>
{
public MyClass() {}
}

I think I need to add a constraint to the Singleton class.
The problem is that I don't want someone to do this:

MyClass foo = new MyClass();

Since the Singleton requires a public constructor, this is actually allowed.
The simple implementation below would not prevent this, would it?


No. No constraint you can put on the singleton would prevent the type
from being constructed "manually". You can only do that by making the
type itself a singleton, rather than providing it *via* a singleton.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 24 '05 #8

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

Similar topics

0
by: Simon Elliott | last post by:
I have a class which uses a singleton to output progress messages for logging or display. It's typically used deep within a hierarchy of aggregations, inheritances and containers. The singleton...
10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
3
by: gary.bernstein | last post by:
I want to call a singleton getInstance function to retrieve a templatized object without knowing what types were used to create the singleton object in the first call to getInstance. How can I do...
4
by: Andrew Ducker | last post by:
I want to make a whole bunch of classes into singletons. They all descend from a base class. I'd like to have code in the base class along the lines of : private This(){ } private static...
0
by: =?Utf-8?B?Sm9obiBXLg==?= | last post by:
I am using Remoting (TCP) to access a singleton object in one app (a service) from another app (call it app #2). Using the methods described in competent literature, the server raises an event...
0
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
How do I handle a generic collection to be sent from the server to the client if that collection must be accessed threadsafe ? my server object must be singleton. my idea of the singleton is:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.