2

Is it better to rely on IOC Framework to implemented singleton? I heard that either double checked locking or relying on static constructor are not good practice, is this true?

13
  • 1
    I get a little tired of the whole "double-checked locking doesn't work" meme. Properly implemented double-checked locking works just fine in .NET ... that is not to say you are going in the right direction. I tend to avoid singletons whenever possible; they are frequently unnecessary. Commented May 6, 2011 at 14:48
  • @Andrew: The OP didn't say that it didn't work. They said they'd heard it was bad practice - which is something I concur with. It's fiddly to get right, and there are almost always simpler approaches which work just as well. (I'm fine with relying on a static constructor if it's doing relatively little work, mind you.)
    – Jon Skeet
    Commented May 6, 2011 at 14:51
  • @Jon Skeet - You are correct; not sure why I read "doesn't work" when that's not what he said. Commented May 6, 2011 at 14:54
  • 1
    @Chris: I've said that volatile makes it work, and that other memory barriers can make it work but that experts disagree about exactly what is required. I'm not going to try to give more information about what to do when experts disagree :) This is the reason I avoid DCL - it's easy to get wrong, and hard to reason about accurately. I like Lazy<T> in .NET 4 which I trust to do the right thing :)
    – Jon Skeet
    Commented May 6, 2011 at 14:58
  • 1
    @Chris: I was trying to explain why I wasn't going into more details. As for other memory barriers: things like calls to Thread.MemoryBarrier.
    – Jon Skeet
    Commented May 6, 2011 at 15:11

4 Answers 4

1

In my opinion the lifetime of an object should be decided by the context creates it not enforced by the type of the object. In common sense not the ways to guarantee "singletonness" of an object are considered bad practice but the usage of the singleton pattern itself.

So, to answer your question: Yes, you should let the IoC container handle the lifetime of its objects.

0

Perhaps this answer may be helpful to you. It contains a basic pattern for a thread safe, lazy-loaded singleton.

0

It's pretty simply to implement the double checked locking pattern, and it's quite succinct and good practice. However, like @Andrew Barber said, improperly implementing it can be painful.

if(_instance == null)
{
    lock(_myLockObject)
    {
         if(_instance == null)
            _instance = new Something();
    }
}

return _instance;
5
  • 1
    I believe _instance should be declared volatile for this to be correct.
    – Jon Skeet
    Commented May 6, 2011 at 14:55
  • I'm curious about this. The MSDN documentation says you should use volatile when not using a lock - msdn.microsoft.com/en-us/library/x13ttww7(v=vs.71).aspx
    – Tejs
    Commented May 6, 2011 at 15:05
  • You're not within a lock when you perform the first check. Memory barriers etc get hideously complicated - there are concerns around the possibility of a reference being published before all the data within the object being published. This kind of thing is why I don't like it.
    – Jon Skeet
    Commented May 6, 2011 at 15:08
  • @Jon, I heard you have to declare not only _instance volatile, but all its properties volatile as well.
    – user705414
    Commented May 6, 2011 at 15:13
  • @user705414: I don't believe so.
    – Jon Skeet
    Commented May 6, 2011 at 15:27
0

Object lifetime as the general rule should always be handled by an IOC container.

Manual object lifetime handling is prone to developer error, SRP violations, and extensive DRY violations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.