# Service Provider

Service providers are the central place for application bootstrapping. It is a pure ES6 class with methods that are used to register bindings and bootstrap application.

# Writing Service Providers

Most service providers contain a register and a boot method. Within the register method, you should only bind things into the service container. You should never attempt to register any event listeners, routes, or any other piece of functionality within the register method.

The Maker CLI can generate a new provider via the make-provider command:

 node maker make-provider MyServiceProvider
const ioc = require("expressweb-ioc");

class MyServiceProvider {
  /**
   * Register application services.
   */
  register() {
    return {
      //
    };
  }

  /**
   * Bootstrap any application services.
   *
   * @return void
   */
  boot() {
    //
  }
}

module.exports = MyServiceProvider;

# The Register Method

The register method is only used to bind things into the ioc container. You should never attempt to register any event listeners, routes, or any other piece of functionality within the register method.

Let's bind our AWS file storage class which interactes with our S3 Bucket as a ServiceProvider with the name FileStorage.

  /**
   * Register application services.
   */
  register() {
    return {
      FileStorage: ioc.asClass("App/Service/AwsFileStorage", "SINGLETON"),
    };
  }

You can bind into the ioc container asClass, asFunction and asValue while specifying the lifetime.

# asClass()

This tells the ioc to instantiate the given class using new.

# asFunction()

This tells the ioc to invoke the given function without any context.

# asValue()

This tells the ioc to provide the given value as-is.

# Lifetime Managment

ExpressWebJs supports lifetime management of instances. This means you can control if an object is resolved and used once, cached within a scope or cached for the life time of the process.

There are 3 lifetime types available.

  1. SINGLETON :The object is resolved once and it's value is cached in the root container.
  2. SCOPED : The registration is scoped to the container - that means the resolved value will be reused when resolved from the same scope or child scope.
  3. TRANSIENT : This is the default. The registration is resolved every time it is needed. This means if you resolve a class more than once, you will get back a new instance every time.

# The Boot Method

The Boot method is called when all providers have been registered, and it's the right place to run any code that you want to be executed at boot time.

const ioc = require("expressweb-ioc");
const hostname = require("../Service/OnBoot");

class MyServiceProvider {

  /**
   * Bootstrap any application services.
   *
   * @return void
   */
  boot() {
    hostname.getHost();
  }
}

module.exports = MyServiceProvider;

# Registering Providers

All service providers are registered in the App/Config/app.js configuration file. This file contains a providers array where you can list the namespace of your service providers.

To register your provider, add it to the array:

/*
    |--------------------------------------------------------------------------
    | Autoloaded Service Providers
    |--------------------------------------------------------------------------
    |
    | The service providers listed here will be automatically loaded on the
    | request to your application. Feel free to add your own services to
    | this array to grant expanded functionality to your applications.
    |
    */
  providers:[
    "App/Providers/AppServiceProvider",
    "App/Providers/RouteServiceProvider",
    "App/Providers/EmailServiceProvider",
  ],