Total Pageviews

Thursday 7 March 2019

AngularJS | Dependency Injection


Dependency Injection ( DI ) :

Dependency is an object of a class which a class needs to function. For example, we have a model class which fetches data from database object. Hence we can say that model class has a dependency on database object.

So what does it mean by Injecting Dependency: It means that the dependency is pushed into the class from the outside i.e. we don’t need to instantiate the dependencies from inside the class using “new” keyword. Instead we should take dependency as parameters using constructors, setters and services.

In a simple word we can say that

“It’s a pattern used to create instances of object and passing into other objects by using constructors, setters and services.”

Or

“Passing more parameters in constructor” is called Dependency Injection.

Also, AngularJs comes with built-in dependency injection mechanism, i.e, it is help us to divide our application into multiple different components which can be injected into each other as dependencies.

Why Dependency Injection ( DI ) :

1. Dependency Injection is a method for writing better code.

2. It decouple the objects which are dependent on each other so that we don’t need to instantiate object using “new” keyword.

3. DI makes testing of components easier.

4. DI increase efficiency of code.

5. It allows the code to be modular.

Example:

Without Dependency Injection:

var empl = function(age, name){
        this.age = age;
        this.name = name;
    }

    function addPerson(){
        var getEmpl = new empl(25, "Swati");
    }

    addPerson();

With Dependency Injection:

    var empl = function(age, name){
        this.age = age;
        this.name = name;
    }

    function addPerson(getEmpl){
      //ToDo
    }
    var getEmpl = new empl(25, "Swati");

    addPerson(getEmpl);


So what we have gotten from the above code is that by using Dependency Injection, we don’t need to change the method every time. Only the passing value will change.

Types of Dependency injection:

1. Constructor Injection – When we provide dependency through the constructor then its constructor injection.


Example:
       var getEmpl = new empl(25, "Swati");

       addPerson(getEmpl);


Here we have passed the values inside “empl” constructor.

2. Setter/Getter Injection - Dependency is provided through setter/getter method.

Example:
class Person{
    private Age ag;
   
    // Setter Based
    void setAge(Age age){
      this.age = age;
    }
    // code 
  }

3.    Interface Injection - Dependencies are provided through methods.

4.  app.factory("addNum", function () {
5.   
6.          return {
7.              add: function (a, b) {
8.                  return a + b;
9.              }
10.        }
11.    });
12. 
13.    app.controller('NumCtrl', CalController);
14.    NumCtrl.$inject = ['$scope', 'addNum'];

Here, instead of passing dependencies as function we will inject the dependencies using the $inject.


Despite being a great concept DI also has some disadvantages:

1. It’s a bit complex concept to learn

2. Over usage can lead to management issues and other problems.

3. Many compile time errors occurs at run-time.

Thanks

No comments:

Post a Comment

Microsoft Logo using flexbox and Reactjs

 <!DOCTYPE html> <html> <head>     <script src="https://unpkg.com/react@18/umd/react.development.js" crossori...