Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/newdash/inject

dependency inject container
https://github.com/newdash/inject

dependency-injection injection ioc typescript

Last synced: 5 days ago
JSON representation

dependency inject container

Awesome Lists containing this project

README

        

# Dependency Inject Container

[![npm (scoped)](https://img.shields.io/npm/v/@newdash/inject?label=@newdash/inject)](https://www.npmjs.com/package/@newdash/inject)
[![Codecov](https://codecov.io/gh/newdash/inject/branch/master/graph/badge.svg)](https://codecov.io/gh/newdash/inject)
[![Netlify](https://img.shields.io/netlify/70db22cf-e923-4aa9-ac52-8591056c70f1?label=docs)](https://newdash-inject.netlify.fornever.org/)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=newdash_inject&metric=alert_status)](https://sonarcloud.io/dashboard?id=newdash_inject)
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=newdash_inject&metric=security_rating)](https://sonarcloud.io/dashboard?id=newdash_inject)

Yet another `dependency injection` container for `typescript`

## Quick Start

```bash
# install libs
npm i -S reflect-metadata @newdash/inject
```

```json5
// tsconfig.json
// remember enable decorator related flags
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
}
```

```ts
// import reflect lib firstly
import "reflect-metadata";
import { inject } from "@newdash/inject"

// a really simple example
it('should support deep constructor injection', async () => {

class A {
v: number
constructor(@inject("v") v) {
this.v = v;
}
}

class B {
a: InjectWrappedInstance
constructor(@inject(A) a) {
this.a = a;
}
}

const ic = InjectContainer.New();
ic.registerInstance("v", 999); // define an instance provider in simple way
const b = await ic.getInstance(B);
expect(b.a.v).toBe(999);

});
```

## [CHANGELOG](./CHANGELOG.md)

## [LICENSE](./LICENSE)