In the previous post, I said that I was going to talk about how to use the Singleton Design Pattern as a simplistic way to introduce a dependency injection container into a project.
One of the best comments I’ve seen about dependency injection containers comes from Stack Overflow (and Joel Spolsky, even):
IoC containers take a simple, elegant, and useful concept, and make it something you have to study for two days with a 200-page manual.
And there’s a time and a post for where I could digress, but that’s not this post. Instead, there are a few nuances about this idea that I want to clarify before I go any further:
- Dependency Injection Containers are more than just ways to store objects. They handle other additional logic. I’ll cover more about this later in the article.
- I don’t recommend sticking with an implementation of the Singleton Design Pattern for a container (or for very many things, for that matter).
- The purpose of showing this as a strategy is a way to show how you can take a project with a tight deadline, a desire to use software development best practices, and find some practical middle ground.
All of that to say is that what I’m going to show is not what I consider being a best practice for using dependency injection containers.
Instead, it’s a way to “meet in the middle” when it comes to working under pressure for building solutions for others all the while not wanting to sacrifice sound engineering principles.