3

I've consistently heard that singletons are generally bad, and most people would look at the below and suggest it should be implemented differently:

ChatManager.Instance.WriteMessage("Hello world");

But what if ChatManager is implemented as:

public class ChatManager
{
  // some methods

  public static ChatManager Instance { get { return DependencyResolver.Resolve<ChatManager>(); } }
}

Based on everything I've heard - basically that singletons should be replaced with dependency resolution for most (stateful) objects - this is now okay, but I have some trouble seeing how it's functionally different from the more traditional singleton approach.

Looking at one of the more popular SO questions on this matter, the accepted answer lists 4 reasons why singletons are bad, of which 2 (#2, #3) are definitely solved using the above. #4 listed is true regardless of whether the dependency is passed in through a constructor, as is often the case for ASP.NET controllers, or resolved via your chosen dependency manager.

So, is this considered bad form, or good software engineering practice?

Community
  • 1
  • 1
Rollie
  • 4,391
  • 3
  • 33
  • 55
  • 1
    "Based on everything I've heard, this is now okay" - what have you heard? Share this with us – Buda Gavril Feb 09 '17 at 08:08
  • 2
    *"singletons are generally bad"* - I find that highly questionable. If singletons were that bad the design pattern wouldn't exist. It's just that a lot of programmers tend to use singletons for *everything*, even when other patterns would be more suitable – UnholySheep Feb 09 '17 at 08:08
  • It depends on what you want to do. In my opinion singeltons are not that bad, if you use them in the right way. – osanger Feb 09 '17 at 08:09
  • 3
    It looks like `DependencyResolver.Resolve()` would ultimately be or rely on another singleton. So you are just pushing the responsibility around a bit with your example. Still Singleton has its use cases if you ask me. – grek40 Feb 09 '17 at 08:10
  • You can refer to this question http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons – Eric Feb 09 '17 at 08:13
  • Does it matter *how* the singleton property is enforced? No, not really. As long as the implementation is safe, there is no difference and it does not make one singleton better than the other. – Would I think that *your* implementation is now better? No, since you moving the singleton enforcement to the dependency resolver (which would be a good thing) but on the other hand add an explicit and hidden service locator use there. Why do you want this `Instance` property to exist in the first place? – poke Feb 09 '17 at 08:15
  • Singletons are good. Static classes with static properties that implement them are not. Your code is very brittle; you can't do any decoration, and testing near impossible. – Enigmativity Feb 09 '17 at 08:19
  • Your code is a) hard to unit test b) is not SOLID (depends on concrete DependencyResolver) c) not thread safe. – zaitsman Feb 09 '17 at 08:35

0 Answers0