Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the type friendly injection?

In the AngularJS documentation, there is an explanation of the differences between a factory, a service, a value, a constant and a provider .

At the end, we have a comparison table: Comparison table One of the rows is "type friendly injection". I could not understand what it is.

What does that mean? Additionally, what does it mean that, in order that a value will have this "type friendly injection", is at the cost of "eager initialization by using new operator directly"?

like image 208
David Michael Gang Avatar asked Sep 04 '14 13:09

David Michael Gang


People also ask

What is dependency injection example?

What is dependency injection? Classes often require references to other classes. For example, a Car class might need a reference to an Engine class. These required classes are called dependencies, and in this example the Car class is dependent on having an instance of the Engine class to run.

What is the use of dependency injection?

Dependency injection is a programming technique that makes a class independent of its dependencies. It achieves that by decoupling the usage of an object from its creation. This helps you to follow SOLID's dependency inversion and single responsibility principles.

What is Java injection?

Dependency Injection in Java is a way to achieve Inversion of control (IoC) in our application by moving objects binding from compile time to runtime. We can achieve IoC through Factory Pattern, Template Method Design Pattern, Strategy Pattern and Service Locator pattern too.


2 Answers

In AngularJS, you can inject dependencies in multiple ways:

  • in the directive link function by position
  • in the directive definition by name
  • in the controller function by name
  • in the factory function by name
  • in the service function by type

Type friendly injection allows you to implicity invoke a constructor function by reference:

myApp.service('Pattern', ["Infinity", RegExp]); 

rather than by explicity using the new keyword:

myApp.factory('Pattern',   ["Infinity", function(Infinity)    {   return new RegExp(Infinity);   }   ]); 

OR

function goInfinity(Infinity)   {   return new RegExp(Infinity);   }   goInfinity.$inject = ["Infinity"]; myApp.factory('Pattern', goInfinity); 

The Service recipe produces a service just like the Value or Factory recipes, but it does so by invoking a constructor with the new operator. The constructor can take zero or more arguments, which represent dependencies needed by the instance of this type.

Eager initialization means that a constant recipe must return a constructor in order to use the aforementioned syntax:

function RegExpConstant()    {   return new RegExp(Infinity);   }   myApp.constant('Pattern', RegExpConstant) 

rather than returning a function, object, or literal value.

The nomenclature comes from Java:

A service is a well-known set of interfaces. A service provider is a specific implementation of a service. A factory is an object that returns an object reference to another object

References

  • Dependency Injection in Angular 2

  • The main goals of Angular 2 and how they will be achieved

  • Vojta Jina: Dependency Injection - NG-Conf

  • AngularJS: Developer Guide - Providers, Service Recipe

  • AngularJS: The Bad Parts

  • Dependency Injection: Syntax Sugar Over Function Composition

  • ServiceFinder (JAX-WS RI)

like image 68
Paul Sweatte Avatar answered Oct 08 '22 21:10

Paul Sweatte


My interpretation of "type friendly injection":

Factories and Providers inject whatever is returned by the factory function and $get function respectively, which could be of any type and could potentially change dynamically at runtime. Whereas Service, Constant and Value injections are of a fixed type that Angular is aware of as it is well defined during the definition of the recipe. Hence they are type friendly injections.

like image 24
Anand Avatar answered Oct 08 '22 19:10

Anand