DEV Community

Discussion on: Explain IoC Container like I'm Five

endy_tj profile image
Endy Tjahjono • Edited

If you don't use IoC container, how do you instantiate objects? Somewhere in the area that starts the application, you would define how to set up the object right?

For example you have IAddProduct. You pass it to the ProductController as constructor argument. The AddProduct concrete class needs IGetUOM. So to create IAddProduct maybe somewhere in your code you have generateIAddProduct that instantiate GetUOM, then instantiate AddProduct, then return it.

IoC container makes this object generation simpler. You tell it if someone ask for IAddProduct, give them AddProduct. You tell it about IGetUOM too, and the other classes and interfaces. Later when something asks the IoC container to generate IAddProduct, it will automatically inspect that AddProduct needs IGetUOM, generates GetUOM, use it to create AddProduct, and then give the complete item to the asker.

If you are interested to learn more about dependency injection, there is a book called Dependency Injection. It's really good!

evieskinner18 profile image
Evie Author

Thank you so much for your clear and accessible explanation! It's not easy explaining these things I know but thank you for having empathy for me in your response 😄