🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

@fluent/di

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fluent/di

Fluent - DI component

1.0.8
latest
Version published
Weekly downloads
21
-8.7%
Maintainers
1
Weekly downloads
 
Created

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.

Keywords

FAQs

Package last updated on 03 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts