Thursday, June 15, 2017

Inversion of Control And Dependency Injection

This post is about what is IOC container? And what is DI?

* Inversion of Control (IoC) means that objects do not create other objects on which they rely to do their work. Instead, they get the objects that they need from an outside source (for example, an xml configuration file).
Giving control to the container to get an instance of the object is called Inversion of Control., means instead of you are creating an object using the new operator, let the container do that for you.
The main tasks performed by IoC container are: to instantiate the application class. to configure the object. to assemble the dependencies between the objects.

* DI means the IoC principle of getting dependent object is done without using concrete objects but abstractions (interfaces). This makes all components chain testable, cause higher level component doesn't depend on lower level component, only from the interface. Mocks implement these interfaces.


There are several basic techniques to implement inversion of control. These are:
  • Using a factory pattern
  • Using a service locator pattern
  • Using a dependency injection of any given below type:

    1). A constructor injection
    2). A setter injection
    3). An interface injection
    * Spring supports only Constructor Injection and Setter/Getter Injection.
Useful/Source links:
Stack Overflow Discussion
Martin Fowler Defintions
Short Video Definition of IOC