# @slicky/di

> Dependency injection for typescript

Latest version **1.1.1** (published 2017-11-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install @slicky/di
pnpm add @slicky/di
yarn add @slicky/di
bun add @slicky/di
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2017-11-08 |
| First published | 2017-02-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | David Kudera |
| Maintainers | slicky |

## Links

- npm: https://www.npmjs.com/package/@slicky/di
- Repository: https://github.com/SlickyJS/Slicky
- Homepage: https://github.com/SlickyJS/Slicky#readme
- Issues: https://github.com/SlickyJS/Slicky/issues
- npm.io page: https://npm.io/package/@slicky/di

## Dependencies (4)

- [@slicky/lang](https://npm.io/package/@slicky/lang.md) ^1.0.0
- [@slicky/utils](https://npm.io/package/@slicky/utils.md) ^1.3.1
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.10
- [@slicky/reflection](https://npm.io/package/@slicky/reflection.md) ^1.0.3

## Recent versions

- 1.1.1 (latest) — 2017-11-08
- 1.1.0 — 2017-11-08
- 1.0.2 — 2017-11-05
- 1.0.1 — 2017-09-04
- 1.0.0 — 2017-02-19

## README

[![NPM version](https://img.shields.io/npm/v/@slicky/di.svg?style=flat-square)](https://www.npmjs.com/package/@slicky/di)

# Slicky/DI

Dependency injection for typescript

## Installation

```
$ npm install @slicky/di
```

Import this code in your bootstrap file:

```typescript
import 'reflect-metadata';
```

## Basic usage

Every service in DI container is ordinary typescript class with `@Injectable()` annotation.

```typescript
import {Container, Injectable} from '@slicky/di';

@Injectable()
class MyService
{
	
	hello()
	{
		console.log('hello');
	}
	
}

let container = new Container;
container.addService(MyService);

let service: MyService = container.get(MyService);
service.hello();
```

If your service depends on another service, it will be automatically autowired.

```typescript
import {Container, Injectable} from '@slicky/di';

@Injectable()
class Logger
{
	
	log(message: string)
	{
		console.log(message);
	}
	
}

@Injectable()
class MyService
{
	
	constructor(private logger: Logger) {}
	
	hello()
	{
		this.logger.log('hello');
	}
	
}

let container = new Container;
container.addService(Logger);
container.addService(MyService);

let service: MyService = container.get(MyService);
service.hello();
```

## Create instance without registering service

Next code will always create new instance of `MyService`.

```typescript
import {Container, Injectable, Inject} from '@slicky/di';

@Injectable()
class Logger
{
	
	log(message: string)
	{
		console.log(message);
	}
	
}

class MyService
{
	
	constructor(@Inject() private logger: Logger) {}
	
	hello()
	{
		this.logger.log('hello');
	}
	
}

let container = new Container;
container.addService(Logger);

let service: MyService = container.create(MyService);
service.hello();
```

## Value service

Useful for configuration options.

```typescript
import {Container, Inject} from '@slicky/di';

class ValueService {}

class Application
{
	
	constructor(@Inject(ValueService) value: string)
	{
		console.log(value);		// output: "some value"
	}
	
}

let container = new Container;
container.addService(ValueService, {
	useValue: 'some value',
});
```

## Factories

```typescript
import {Container} from '@slicky/di';

class MyService {}

let container = new Container;
container.addService(MyService, {
	useFactory: () => new MyService,
});
```

## Aliases

```typescript
import {Container, Injectable} from '@slicky/di';

@Injectable()
class OldService {}
class MyService {}

let container = new Container;
container.addService(OldService);
container.addService(MyService, {
	useExisting: OldService,
});
```

## Using different class

```typescript
import {Container, Injectable} from '@slicky/di';

class MyService {}
class MyBetterService {}

let container = new Container;
container.addService(MyService, {
	useClass: MyBetterService,
});
```

## Forking containers

You can simply create fork of current container with for example some overwritten services.

```typescript
import {Container} from '@slicky/di';

let container = new Container;
let fork = container.fork();
```

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