# Service provider

# Concept

Service provider aims to configure your service, especially :

  • declare handlers
  • declare container binding (IOC configuration)
  • declare middlewares
  • etc...

# Recipes

# Container bindings

  @Container.serviceProvider({
    providers: [
      [MyInterfaceResolver, MyImplementation], // bind MyInterfaceResolver to MyImplementation
      MyIocReadyClass, // equivalent to [MyIocReadyClass, MyIocReadyClass]
    ],
  })
1
2
3
4
5
6

You can add your bindings in the providers keyword in service provider decorator. By example, you can bind ConfigProviderInterfaceResolver to ConfigProvider. By doing this, if you add ConfigProviderInterfaceResolver in your constructor, the container will inject ConfigProvider.

Note: you always can acces to the current container with this.getContainer() to write custom bindigs.

# Child services providers declaration

  @Container.serviceProvider({
    children: [
      ChildServiceProvider,
    ],
  })
1
2
3
4
5

You can add child service provider. In this case, ChildServiceProvider will access all of the bound class of the parent container. By example, if you have declared some provider inside the root service provider, the child can use them. It can override it by declaring itself another alias.

# Declare handlers

  @Container.serviceProvider({
    handlers: [
      MyCustomAction,
    ],
  })
1
2
3
4
5

Every handlers declared in the service provider will be callable.

# Bind middleware

  @Container.serviceProvider({
    middlewares: [
      ['can', PermissionMiddleware],
    ],
  })
1
2
3
4
5

This shorcut allow you to bind a string ('can') to a middleware class (PermissionMiddleware). By doing this, you can use the middleware in your action without importing it.

# Example

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

@Container.serviceProvider({
  providers: [
    [MyInterfaceResolver, MyImplementation], // bind MyInterfaceResolver to MyImplementation
    MyIocReadyClass, // equivalent to [MyIocReadyClass, MyIocReadyClass]
  ],
  children: [
    ChildServiceProvider,
  ],
  handlers: [
    MyCustomAction,
  ],
  middlewares: [
    ['can', PermissionMiddleware],
  ],
})
export class ServiceProvider extends Parents.ServiceProvider {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18