Overview
Provider

Providers are responsible for handling dependency injection.

Supported Provider Types

All Provider decorator has token and scope as arguments. The token is a token that need to be unique for a Provider and can be used with @Inject decorator.

@Provider

@Provider is required for dependency injection


Create a file in src/providers directory to create a a provider:

  • src/providers/todos.provider.ts
import { Provider, Scope } from '@heronjs/common';
 
@Provider({
    token: 'todo.provider',
    scope: Scope.SINGLETON,
})
export class TodoProvider {
    //...logic here
}

@Repository

@Repository the alias of @Provider for Repository


Create a file in src/providers directory to create a a repository:

  • src/providers/todos.repository.ts
import { Repository, Scope } from '@heronjs/common';
 
@Repository({
    token: 'todo.repository',
    scope: Scope.SINGLETON,
})
export class TodoRepository {
   //...logic here
}

@Dao

@Dao is the alias of @Provider for Data Access Object


Create a file in src/providers directory to create a a dao:

  • src/providers/todos.dao.ts
import { Provider, Scope } from '@heronjs/common';
 
@Dao({
    token: 'todo.dao',
    scope: Scope.SINGLETON,
})
export class TodoDAO {
    //...logic here
}

Register Providers

Add TodoProvider class to @Module({}) providers property to enable the routes

@Module({
    providers: [TodoProvider],
})
export class AppModule {}

Inject a Registered Provider

Let's assume you need to create a GET method api that get all todos and we need to inject TodoProvider that we created above.

import { Rest,Inject } from '@heronjs/common';
 
@Rest('/todos')
export class TodoController {
 
    constructor(@Inject('') private readonly _provider: TodoProvider) {
 
    }
 
    @Get({ uri: '/'})
    public async findAll(): Promise<TodoModel> {
       return this._provider.findAll();
    }
}

Supported Scopes

  • Transient
  • Container
  • Singleton
  • Resolution