One of the AngularJS features that causes the most confusion is dependency injection (DI). It can be hard to figure out what DI is, how it works, and why it is useful. Even if you have encountered dependency injection in other frameworks like ASP.NET MVC, AngularJS takes an unusual approach and mixes in some features that are distinct from other languages. As you will learn an AngularJS application consists of different components: controllers, directives, filters, and so on.Continue Reading>>
So, there are a lot of confusion in developers mind regarding Dependency Inversion Principle, Dependency Injection and Inversion of Control, that’s why I tried to explain as par my experience. We need to understand a bit of Software Design Principle & Software Design Pattern. Dependency Inversion Principle (DIP) is a Software design principle and Inversion of Control (IoC) is a Software design pattern. Let's see what is Software design principle and pattern.
Software Design Principle:
Principle provides us only guideline to achieve some task. Principle tells us what is right and what is wrong. It doesn’t say us how to solve problem. It just gives some guideline so that we can design good software and avoid bad design. Some principles are DRY, OCP, DIP etc.Continue Reading>>
Inversion of control (IoC) describes a design in which custom-written portions of a computer program receive the flow of control from a generic, reusable library. A software architecture with this design inverts control as compared to traditional procedural programming: in traditional programming, the custom code that expresses the purpose of the program calls into reusable libraries to take care of generic tasks, but with inversion of control, it is the reusable code that calls into the custom, or problem-specific, code.
Let's try to understand the inversion of control in with an example. Suppose, we have a console application with a sequence of commands like "Enter name", "enter address"; this program would drive the prompts and pick up a response to each one. With graphical (or even screen based) UIs the UI framework would contain this main loop and your program instead provided event handlers for the various fields on the screen. The main control of the program was inverted, moved away from you to the framework.
We have discussed Dependency Injection (DI) as a separate article; here we only try to know the core concept of Dependency Injection (DI). A type of IoC where we move the creation and binding of dependency outside of the class that depends on it. In normal object are created inside of the dependent class and bounded inside the dependent class. In Dependency Injection (DI) it is done from outside of the dependent class. There are three type of Dependency Injection (DI).
- Setter Injection
- Interface Injection