One of the things to watch out for when working with DI Containers is being aware of the Dependency Graph. One of the more dangerous types is where you expect a particular object to exist once during an entire HTTP Request lifecycle, but it starts behaving like a singleton.
Tag: Design Patterns
Singleton pattern gives a way of ensuring that there is only a single instance of a particular object – but why has the singleton become an anti-pattern? Well, the main issue with singleton is that it becomes very difficult to test since it is very likely that it’s tightly coupled to a something within the system – it’s just the nature of the singleton pattern, since it has to be able to create itself, and in order to do so, it needs to be able to gather everything it depends on by itself – not to mention issues with thread-safety if not done right.