This package has been deprecated

Author message:

Not maintained anymore

@fluent/di
TypeScript icon, indicating that this package has built-in type declarations

1.0.8 • Public • Published

Dependency injection

Fluent use reflect-metadata for reflection and you need two conditions for proper functioning:

  • you must add "experimentalDecorators": true to your tsconfig.json file
  • you must add "emitDecoratorMetadata": true to your tsconfig.json file

This component has an identical implementation approach as ASP.NET Core and has a number of inconveniences.

The reflection can not be realized without decorators and therefore it is necessary to use the @Injectable() decorator for all classes to use dependency injection.

Example of usage:

@Injectable()
export class PingService {
}

This decorator only emit metadata and does not process anything.

The service collection is carried out with the help of the ServiceCollection class and it has three main methods that add the right lifetime:

  • addSingleton() - an instance of a service is always the same
  • addScope() - an instance of a service is always the same if its call occurs in one scope
  • addTransient() - the service instance is always unique

Example of usage:

@Main()
export class Startup {
  configureServices(services: ServiceCollection) {
    services.addSingleton(PingService);
  }
}

You can also get an instance of the service in the constructor:

@Injectable()
export class PongService {
  constructor(pingService: PingService) {
  }
}

When calling PongService the instance will receive PingService as constructor argument.

Readme

Keywords

Package Sidebar

Install

npm i @fluent/di

Weekly Downloads

9

Version

1.0.8

License

MIT

Last publish

Collaborators

  • eemeli
  • zbraniecki
  • tatumcreative
  • flod