This may sound like a stupid question, but can DI be used everywhere where a Singleton is needed? Or are there use cases where a Singleton makes more sense? A professor of mine said that there a few but valid cases where a Singleton is “good enough” but I’m somehow not happy with that :-/.
Share
One of the topics in DI is the lifetime of the object which will be injected. Example lifetimes include Singleton as well as Transient, HttpContext, ThreadLocal, Custom etc… So when using DI you could specify an object to be of Singleton lifetime, may be a config class which gets populated at application start. This would seem like a nice class to have as a Singleton.
The Singleton pattern is a powerful one, but like all design patterns they can be more harm than good when used in an incorrect manner. DI and the avoidance of the Singleton also gives rise to better testability.
Cheers for now,
Andrew