# attr-di

> Simple dependency injection using attributes for TypeScript applications

Latest version **1.1.3** (published 2021-12-29) · ISC license · 0 weekly downloads

## Install

```sh
npm install attr-di
pnpm add attr-di
yarn add attr-di
bun add attr-di
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.1.3 |
| Published | 2021-12-29 |
| First published | 2019-12-29 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 14.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Andrew M. Fahmy |
| Maintainers | andrewmfahmy |

## Links

- npm: https://www.npmjs.com/package/attr-di
- Repository: https://github.com/AndrewFahmy/attr-di
- Homepage: https://github.com/AndrewFahmy/attr-di#readme
- Issues: https://github.com/AndrewFahmy/attr-di/issues
- npm.io page: https://npm.io/package/attr-di

## Dependencies (1)

- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ~0.1.13

## Recent versions

- 1.1.3 (latest) — 2021-12-29
- 1.1.2 — 2020-11-29
- 1.1.1 — 2020-11-29
- 1.1.0 — 2020-11-14
- 1.0.5 — 2020-06-02
- 1.0.4 — 2020-02-13
- 1.0.3 — 2020-02-13
- 1.0.2 — 2020-01-07
- 1.0.1 — 2020-01-07
- 1.0.0 — 2019-12-29

## README

# Attr-DI
Attribute based dependency injection library for TypeScript applications

## Installation

Install using **npm**

```
npm install attr-di --save
```

## Configuration
You first need to configure your project **tsconfig.json** file so that **attr-di** can work properly. You need to enable decorators and emit metadata as well

```json
"experimentalDecorators": true,
"emitDecoratorMetadata": true
```


## Usage
Select a class and before adding a decorator you need to select an injection type. Currently this library supports the following Lifetime injection types*

1. **Transient** a new instance will be created.
2. **Singleton** only a single copy will be created on first time it's requested (lazy loading) then this instance will be used throughout the application lifetime.

After selecting a type, delcaring a decorator is pretty easy

```typescript
import { Injectable, InjectionTypes } from "attr-di";

@Injectable(InjectionTypes.Singleton)  // <-- Decorator declaration
export class Service1 {
    ...
}
```


## Injection Patterns
The library can inject an instance using one of 2 patterns

### &nbsp;**Constructor Injection**
&nbsp; This is the most famous type of injection basically all you need to do is to define a variable inside the constructor, just make sure that all created variables are for types that have an `@Injectable()` decorator, otherwise the library won't be able to create an instance

```typescript
export class Component{
    constructor(private service : Service1){}
    ...
}
```


### &nbsp;**Variable Injection**
&nbsp; This below example creates an instance manually when needed.

```typescript
import { injectorInstance } from 'attr-di';

const component = injectorInstance.getInstance<Component>(Component);
```

```typescript
import { injectorInstance } from 'attr-di';

const component = injectorInstance.getInstanceWithParameter<Component, Model>(Component);
```

In first example, you pass the requested instance type as the function parameter, and the required return type as the generic parameter.

As for the second example, you can pass an extra generic paramter which can be passed to generic instances like ``Component<T>``.

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