2

Possible Duplicate:
How to implement singleton without using static/global variable? Possible?

One of the reason for most people saying that Singleton is evil is that it provides a global point of access or it becomes like a global variable. Is it possible to implement a singleton without having any kind of global variable including static. Is it possible to do this in C#.NET ?

3
  • 1
    Register the singleton instance with your IoC container in a singleton context and use DI to pass it to the consuming objects. (Obviously there will still be global variables and entry points, but most of your program won't see them) Commented Nov 1, 2012 at 14:17
  • 4
    A Singleton isn't necessarily evil. It really depends on how you use it... Commented Nov 1, 2012 at 14:18
  • In general gobal variables are a bad thing. The purpose of a singleton is to provide a global variable while protecting any internal information, therefore making sure no user can corrupt the data someone else is using. So the singleton makes using a global variable less bad. But still the essence of a Singleton is that it's a global variable. So use a Singleton when you need a global variable (or for any of the other reasons). And if you don't need a global variable, don't use one. Commented Nov 1, 2012 at 14:28

4 Answers 4

3

The problem you are hinting at is that a Singleton becomes like a global var. What it uses is irrelevant.

And the answer would be No. You will always need a static somewhere, you can only defer it. And that kind of defeats the point.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, the answer would be yes because there are other possible mechanisms, besides a static variable, of creating global state, but the first part is right in that it will have all of the same (if not more) drawbacks regardless of the mechanism of creating that global state.
What methods would that be (that do not use a static under the covers) ?
Storing the data in a flat file, using a register, a database, using a web service to get/set the values, which itself could store it in any number of places, etc.
2

No, that wouldn't be a singleton then.

You can use an IoC container to only inject things as a singleton.

1 Comment

A singleton is a class for which there is never more than one instance of; it doesn't require it to have any static variables.
2

You could implement an interface and then use something like StructureMap. In SM you would build the class like this:

public class Foo : IFoo
{
}

And then you would define, in your container, the following:

ObjectFactory.Initialize(x =>
{
    x.For<IFoo>.Singleton().Use<Foo>();
}

Then you would inject this resource where you needed it:

public class Bar
{
    public Bar(IFoo foo)
    {
    }
}

So, now there will only ever be one instance of the concrete created and then injected into an enumerable number of types that need it.

5 Comments

This is a really good approach because the user of a singleton isn't tied to a concrete implementation, making unit testing easier since you're breaking dependency on a concrete class.
@StealthRabbi, absolutely correct! Unit testing is something I have to constantly think about so I build a lot of interfaces. :)
This is not a singleton, you can have more instances of Foo.
@peer, true, but it's assumed that anyone using IFoo would do so via StructureMap. Also, Foo itself can be more easily tested since Foo itself doesn't implement the Singleton pattern. Singletons are tough to UT. You may want to define Foo as an internal class of some auxiliary project, so no one can actually instantiate it outside the project. The StructureMap setup would need to be done in that project though.
@peer, as StealthRabbi pointed out, in theory yes there could be more than one instance created. However, if you move to DI across your project it would not be an issue in practice. I have never experienced an issue with this in practice because when you're using DI, that's what you use, and so this works perfectly. And has the added benefit of easily UT.
0

I don't believe so. If there isn't a static variable, where do you store the instance? Normally when I want a singleton of a class, I make all the constructors private and provide a static method or property that will create the instance if it hasn't already been created, and return the instance. The instance itself is stored as a private variable, so the only way to access it is through the provided method/property -- or reflection.

1 Comment

There are ways, beyond a static variable, of having global state; a static variable is just the most convenient and effective in this context.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.