Inversion of Control (IOC) and Dependency Injection (DI) are related concepts in software development, but they are not the same thing.
IOC (Inversion of control) is the design target intended to achieve between the classes and their dependencies,
In other words a class should not create or control the objects it uses, but instead should rely on an external source to provide those objects. This is typically achieved by using a container or framework that creates objects and manages their lifetimes
DI (Dependency Injection) is one of the design patterns used to achieve this target
There are other ways to achieve IOC which is subjected to the application
As the name suggests the flow of controls is inverted to improve the performance, maintainability and scalability.
Simply put (DI) is a specific implementation of IOC where a class's dependencies (i.e. the objects it uses) are provided to it by an external source, rather than the class creating or controlling them directly. This is typically achieved by using a DI container or framework, which can be configured to create and inject the required objects into a class.
In other words, IOC is a pattern, and DI is one way of implementing that pattern. You can use IOC without using DI.
In
this example, an instance of the IDependency interface is passed to the
constructor of the DIClass class. The DIClass class does not create or
control the IDependency object, but instead relies on an external source
to provide it.
Comments