Angular - Providers

All services in Angular are singletons. That means that the injector uses each recipe at most once to create the object. The injector then caches the reference for all future needs.


http://blog.xebia.com/2013/09/01/differences-between-providers-in-angularjs/

Two ways to define providers

  1. angular.module().config( .. )
  2. angular.module._service_type_() - where _service_type_ is one of the below 6.

Different ways to create a Provider

  1. Constant - angular.module.constant("referredBy", constObj);
    • A constant cant be intercepted by a decorator and hence its value cant be changed.
  2. Value - angular.module.value("referredBy", valueObjOrFn)
    • value can be string, number or a function.
    • Value cant be injected into configurations but can be intercepted by decorators
  3. Service - angular.module.value("referredBy", serviceConstructorFn);
    • A service is an injectable constructor.
    • Should always be a function
    • The service instance is created using the Constructor Function pattern - new Service()
    • The return type of Service is fixed( in default behavior )
  4. Factory - angular.module.value("referedBy", serviceFn);
    • A factory is an injectable function
    • A factory can return any type
    • A Factory is a provider with only a $get() method
    • The service instance is created by calling the Factory pattern - serviceInstance = Function(); The return value from the Function is the service instance.
  5. Decorator - 
    • angular.module.config( function( $provide ){ $provide.decorator( 'otherProvider', function(){}) } );
    • A decorator can modify or encapsulate other providers. There is one exception and that a constant cannot be decorated.
  6. Provider
    • A provider is the most sophisticated method of all the providers. It allows you to have a complex creation function and configuration options. 
    • A provider is actually a configurable factory. 
    • The provider accepts an object or a constructor.

app.provider('movie', function () {
  var version;
  return {
    setVersion: function (value) {
      version = value;
    },
    $get: function () {
      return {
          title: 'The Matrix' + ' ' + version
      }
    }
  }
});


Summary


  • All the providers are instantiated only once. That means that they are all singletons.
  • All the providers except constant can be decorated.
  • A constant is a value that can be injected everywhere. The value of a constant can never be changed.
  • A value is just a simple injectable value.
  • A service is an injectable constructor.
  • A factory is an injectable function.
  • A decorator can modify or encapsulate other providers except a constant.
  • A provider is a configurable factory.





Service vs factory vs Provider
------------------------------------------
Factory - The service instance is created by calling the Factory pattern - serviceInstance = Function(); The return value from the Function is the service instance.

Service - The service instance is created using the Constructor Function pattern - new Service()

Provider - This is the verbose version which provides the options to configure the services before instantiation by Angular. used rarely. The behavior will vary from application to application based on the configuration within the application.
Service returned is by calling - ProviderFunction().$get()


provider - A provider is an object with a $get() method. The injector calls the $get method to create a new instance of a service. The Provider can have additional methods which would allow for configuration of the provider.

No comments:

Post a Comment