open-index/open-npm huggingface dataset:
npm package: @fluent/di
Description: Fluent - DI component
Author: Eugen Melnychenko
Latest version: 1.0.8
Deprecated: Not maintained anymore

# 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.