# @hacker-und-koch/di

> ## Basic Idea

Latest version **0.2.1** (published 2021-07-14) · LGPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install @hacker-und-koch/di
pnpm add @hacker-und-koch/di
yarn add @hacker-und-koch/di
bun add @hacker-und-koch/di
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2021-07-14 |
| First published | 2020-03-22 |
| Weekly downloads | 0 |
| License | LGPL-3.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 68.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | hacker-und-koch |

## Links

- npm: https://www.npmjs.com/package/@hacker-und-koch/di
- Repository: https://github.com/hacker-und-koch/ts-node-additions
- Homepage: https://github.com/hacker-und-koch/ts-node-additions#readme
- Issues: https://github.com/hacker-und-koch/ts-node-additions/issues
- npm.io page: https://npm.io/package/@hacker-und-koch/di

## Dependencies (4)

- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13
- [@hacker-und-koch/util](https://npm.io/package/@hacker-und-koch/util.md) 0.2.1
- [@hacker-und-koch/getopt](https://npm.io/package/@hacker-und-koch/getopt.md) 0.2.1
- [@hacker-und-koch/logger](https://npm.io/package/@hacker-und-koch/logger.md) 0.2.1

## Recent versions

- 0.2.1 (latest) — 2021-07-14
- 0.2.0 — 2021-07-14
- 0.1.14 — 2021-07-14
- 0.1.13 — 2021-07-14
- 0.1.8 — 2021-07-13
- 0.0.7 — 2020-05-31
- 0.0.6 — 2020-05-21
- 0.0.5 — 2020-04-29
- 0.0.4 — 2020-04-25
- 0.0.3 — 2020-03-22
- 0.0.2 — 2020-03-22
- 0.0.1 — 2020-03-22

## README

# @hacker-und-koch/di

## Basic Idea

Dependency injection allows to easily distribute class instances across an application runtime. It also supports comfortable mocking in unit tests and strong control for staged instantiation.

## Minimum Viable Example [[example](./src/examples/minimal.ts)]

```typescript
import { Application, bootstrap } from '@hacker-und-koch/di';

@Application()
class App { }

bootstrap(App);
```

## Hooks [[example](./src/examples/hooks.ts)]
```typescript
@Injectable()
class Worker implements 
    OnConfigure, 
    OnInit, 
    OnReady, 
    OnDestroy 
{
    async onConfigure() {}
    async onInit() {}
    onReady() {}
    async onDestroy() {}
}
```

## Logger [[example](./src/examples/logger.ts),[details](../logger)]
```typescript
@Injectable()
class MyWorker {

    constructor(private logger: Logger) {
        this.logger.info("Hi"); 
        // 2020-01-01T10:10:45.1234 (i) MyWorker: Hi
    }
}
```

## GetOpt [[example](./src/examples/getopt.ts),[details](../getopt)]
```typescript
@Injectable()
class MyWorker implements OnReady {

    constructor(private getopt: GetOpt) {}

    onReady() {
        this.getopt.option("host"); // "foo.com"
    }
}
```
```bash
$ yarn start --host=foo.com
```
### Configuring GetOpt
TODO; [meanwhile](../getopt)


## More Depth [[example](./src/example/worker-and-app.ts)]
```typescript
@Injectable()
class InnerWorker {}

@Injectable()
class OuterWorker {
    constructor(private worker: InnerWorker) {}
}

@Application({
    declarations: [
        InnerWorker,
        OuterWorker,
    ]
})
class App {    
    constructor(private worker: OuterWorker) {}
}
bootstrap(App, {
    log: "info"
});
```
leads to output:
```
<timespamp> (i) bootstrap: Target: App
<timespamp> (i) Providers: Created new instance of Logger for Providers.
<timespamp> (i) Providers: Creating new instance of GetOpt
<timespamp> (i) Providers: Creating new instance of InnerWorker
<timespamp> (i) Providers: Creating new instance of OuterWorker
<timespamp> (i) Providers: Configuring 5 instances
<timespamp> (i) Providers: Done configuring
<timespamp> (i) Providers: Initializing 5 instances
<timespamp> (i) Providers: Initialization complete
<timespamp> (i) Providers: Announcing ready state to 5 instances
<timespamp> (i) bootstrap: Done bootstraping App.
```

## Shared and Unique Instances [[example](./src/examples/shared-instance.ts)]
Instances can not only be injected via `constructor` arguments, but also via the `@Inject` decorator. This method also allows to provide a custom instance id. Therefore you can have classes instanciated multiple times and reused on demand.
```typescript
@Injectable()
class OuterWithSharedInstance {

    @Inject(Worker, 'foo')
    private worker1: Worker;

    @Inject(Worker, 'foo')
    private worker2: Worker;
}
```
```typescript
@Injectable()
class OuterWithUniqueInstances {

    @Inject(Worker, 'foo')
    private worker1: Worker;

    @Inject(Worker, 'bar')
    private worker2: Worker;
}
```

## Passing configs [[example](./src/examples/config.ts)]
```typescript

interface MyWorkerConfig {
    myOption: string;
}

@Injectable()
class MyWorker implements OnConfigure {

    @InjectConfiguration()
    private config: MyWorkerConfig;

    onConfigure() {
        console.log(`My option is: ${this.config.myOption}`);
    }
}

@Application({
    declarations: [MyWorker],
    configs: [config<MyWorkerConfig>(MyWorker, {
        myOption: 'foo',
    })]
})
class MyApp {
    constructor(worker: MyWorker) { }
}
```

## Bootstrap Options
TODO

---
_Source: https://npm.io/package/@hacker-und-koch/di · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
