So today, I was assigned with a task at work to rewrite an existing Groovy service that handles deduplication logic for our notification service. The application was bootstrapped using Spring Boot, and I stumbled upon this statement in the docs.
IoC is also known as dependency injection (DI)
^ That doesn’t seem right. The author of the docs probably meant something else.
IoC is an excellent design principle that allows focussing on decoupling your execution in a dynamic application flow. Dependency Injection is a technique that involves passing a service’s dependency during runtime instead of the service building the dependency on its own. Cool so far, but they obviously aren’t one and the same. IoC is a principle and DI is a technique. There are several other techniques to achieve IoC such as using a service locator pattern (a registry is pinged which returns the necessary information to the service) or using template methods.
I understood why the statement that IoC is also known as dependency injection makes better sense to me now. I stumbled upon that line in the IoC container section of the docs. IoC is a broader term but when it comes to using containers that take control of providing the required objects to the application, dependency injection better describes how IoC is being achieved. DI can be achieved with or without using containers but it gets very difficult to manage multiple dependancies being injected into the application within the client code.
I think the way IoC containers handle orchestrating these dependency injections is very complicated and its difficult for most developers to initially understand the concept. Martin Fowler wrote an amazing article to help understand the principles underlying the IoC containers and it made me realize it is a very good practice to use IoC containers if we are trying to perform dependency injection on the client code. The user interface anecdote in the article should explain how containers are a clean non-invasive tools to inject dependencies into the code.
Here’s the link to the article, highly recommended — if you want to know something new about dependency injection pattern: https://martinfowler.com/articles/injection.html