Subject: Inverse of Control (IoC) & Dependency Injection (DI)
Author: WebSpider
Posted on: 10/20/2019 09:26:48 PM
Inversion of Control (IoC) is the principle where the control flow of a program is inverted: instead of the programmer controlling the flow of a program, the external sources (framework, services, other components) take control of it. It's like we plug something into something else.
Without IoC, the flow is for object to call certain library and expect certain results returned; with IoC, a framework is controlling the flow by predefined interfaces -- all job left for developers is just to provide the customized implementations (or plugins) for those interfaces according to specific needs. The developers do not (or even need to) know when and how those plugins are called. A good example is the Spring MVC Framework.
Dependency Injection (DI) is a form of IoC -- the dependency of an object is inverted: instead it controls its own dependencies, life cycle, the object only USE the dependencies which are injected from external sources (framework, services, other components).
Just like in coding, we seldom hard-code the value of STRING but we rely on the configuration to provide the flexibility or usability of our software product. A good example is the Database URL STRING.
Move one step further from variable STRING to class OBJECT -- instead of hard-coding the OBJECT we usually build business or service logic on interfaces and rely on the injections for flexibility.
References: