# Service Provider
# Introduction
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:
import { ServiceProvider } from "Elucidate/Support/ServiceProvider";
export class MyServiceProvider extends ServiceProvider {
/**
* Register application services.
* @return void
*/
public register(): void {
//
}
/**
* Bootstrap any application services.
* @return void
*/
public async boot(): Promise<void> {
//
}
/**
* Load any service after application boot stage
* @return void
*/
public async booted(): Promise<void> {
//
}
}
# 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 fileStorageInterface to AwsFileStorage implementation which interacts with our S3 Bucket.
import {awsFileStorage} from "App/Service/Impl/awsFileStorage";
import {fileStorageInterface} from "App/Service/fileStorageInterface
import {ServiceProvider} from "Elucidate/Support/ServiceProvider";
export class MyServiceProvider extends ServiceProvider {
/**
* Register application services.
* @return void
*/
register(): Promise<void> {
this.bindAsSingletonClass(fileStorageInterface, awsFileStorage);
}
}
The first parameter is the abstract implementation, followed by the concrete implementation. This now helps us to use the abstract implementation in place of the concrete implementation.
# Lifetime Management
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 2 lifetime types available.
SINGLETON :
The object is resolved once and it's value is cached in the root container.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 has 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";
export class MyServiceProvider extends ServiceProvider{
/**
* Bootstrap any application services.
* @return void
*/
public boot(): Promise<void> {
hostname.getHost();
}
}
# The Booted Method
The Booted method is called after application boot stage.
import {ServiceProvider} from "Elucidate/Support/ServiceProvider";
import hostname from "../Service/OnBoot";
export class MyServiceProvider extends ServiceProvider{
/**
* Load any service after application boot stage
* @return void
*/
public async booted(): Promise<void> {
hostname.getHost();
}
}
# 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 providers array:
/*
|--------------------------------------------------------------------------
| Auto load 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",
],