Dependency Injection is of 2 types.
- Constructor InjectionConstructor Injection, ensures that all the dependency objects are initialized before we attempt to invoke any methods or peroperties, thus avoiding null reference exceptions.
- Setter InjectionSetter Injection, enables you to inject the object, just before you need it. So, if the object is an expensive object to create, and you want to create it as late as possible
- Dependency Injection allows us to develop very loosely coupled systems.
- Easy to swap in a different implementation of a component, as long as the component implements the interface type.
- Dependency Injection, allows objects to be mocked with in the Unit Tests. This is the greatest advantage of Dependency Injection.