# 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:

 ts-node maker make-provider MyServiceProvider
import ServiceProvider from "Elucidate/Support/ServiceProvider";

class MyServiceProvider extends ServiceProvider{
  /**
   * Register application services.
   * @return void
   */
  register() {
    //
  }

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

  /**
   * Load any service after application boot stage
   * @return void
   */
  public async booted() {
    //
  }
}

export default 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.

   import AwsFileStorage from "App/Service/AwsFileStorage";
   import ServiceProvider from "Elucidate/Support/ServiceProvider";

  class MyServiceProvider extends ServiceProvider{
    /**
     * Register application services.
     * @return void
     */
    register() {
      this.app.singleton("FileStorage", AwsFileStorage, "class");
    }
  }

  export default MyServiceProvider;

The first parameter is the service name, followed by the implementation and lastly the mode which can be class, function, or value

# As Class()

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

# As Function()

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

# As Value()

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. REGISTER : 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.

import ServiceProvider from "Elucidate/Support/ServiceProvider";
import hostname from "../Service/OnBoot";

class MyServiceProvider extends ServiceProvider{

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


export default MyServiceProvider;

# The Booted Method

The Booted method is called after application boot stage.

import ServiceProvider from "Elucidate/Support/ServiceProvider";
import hostname from "../Service/OnBoot";

class MyServiceProvider extends ServiceProvider{

  /**
   * Load any service after application boot stage
   * @return void
   */
  public async booted() {
    hostname.getHost();
  }
}


export default MyServiceProvider;

# Registering Providers

All service providers are registered in the Config/app.ts 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",
  ],