# Basics

You may create your own provider. In order to do that, you may create a simple decorated class as:

import { Container } from '@ilos/core';

@Container.provider()
class MyProvider {
  doSomething() {
    //
  }
}
1
2
3
4
5
6
7
8

You can register this provider in your service provider as :

import { Container, Parents } from '@ilos/core';
import { MyProvider } from './providers/MyProvider';

@Container.serviceProvider({
  providers: [
    MyProvider,
  ],
})
1
2
3
4
5
6
7
8

And now, use it everywhere by adding it to the constructor

  constructor(
    private myProvider: MyProvider,
  )
1
2
3

# Recipes

# Custom identifier

If your provider may have several implementation (or you want to have a losely coupled architecture, which should be a good idea), you can add an identifier to your provider. You can do it two ways (see after). By doing this, you may use it by injecting MyProviderInterfaceResolver instead of MyProvider in the constructor.

  constructor(
    private myProvider: MyProviderInterfaceResolver,
  )
1
2
3

# Using the provider decorator (prefered)

import { Container } from '@ilos/core';
import { MyProviderInterface, MyProviderInterfaceResolver } from '../interfaces/MyProviderInterface';

@Container.provider({
  identifier: MyProviderInterfaceResolver,
})
class MyProvider implements MyProviderInterface {
  doSomething() {
    //
  }
}
1
2
3
4
5
6
7
8
9
10
11

# Using the service provider decorator

import { Container, Parents } from '@ilos/core';
import { MyProvider } from './providers/MyProvider';
import { MyProviderInterfaceResolver } from './interfaces/MyProviderInterface';

@Container.serviceProvider({
  providers: [
    [MyProviderInterfaceResolver, MyProvider],
  ],
})
1
2
3
4
5
6
7
8
9

# Hooks

You may use hook on your provider. It should implements RegisterHookInterface, InitHookInterface, or DestroyHookInterface.

# Shared providers

Sometimes, you may have a provider which is used by several services. You can create a package to shared it.

# Suggested directory structure

providers
│
└───custom
    |   package.json
    |   tsconfig.json
    |   ...
    └─── src
        │   CustomProvider.ts
        |   CustomProvider.spec.ts
        |   index.ts
        └─── interfaces
            |   CustomProviderInterface.ts
1
2
3
4
5
6
7
8
9
10
11
12

# Base

CustomProvider.ts defines the exported methods of the provider.

@Container.provider({
  identifier: CustomProviderInterfaceResolver
})
export class CustomProvider implements CustomProviderInterface {
    public doSomething(params:any): any {
        return data;
    }
}
1
2
3
4
5
6
7
8

# Interface and Interface resolver

import { Interfaces } from '@ilos/core';

export interface CustomProviderInterface extends Interfaces.ProviderInterface{
  doSomething(params:any): any;
}

export abstract class CustomProviderInterfaceResolver implements CustomProviderInterface {
  doSomething(params:any): any {
    throw new Error();  
  }
}
1
2
3
4
5
6
7
8
9
10
11

# Export

index.ts defines the exported classes

export { CustomProvider } from './CustomProvider';
export { CustomProviderInterface, CustomProviderInterfaceResolver } from './interfaces/CustomProviderInterface';
1
2