Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/midwayjs/injection
Injection is a powerful inversion of control container that is widely used in the midway framework and brings good user experience.
https://github.com/midwayjs/injection
Last synced: about 1 month ago
JSON representation
Injection is a powerful inversion of control container that is widely used in the midway framework and brings good user experience.
- Host: GitHub
- URL: https://github.com/midwayjs/injection
- Owner: midwayjs
- License: mit
- Created: 2019-01-21T06:12:05.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-12-28T16:29:32.000Z (almost 3 years ago)
- Last Synced: 2024-04-23T22:09:42.646Z (8 months ago)
- Language: TypeScript
- Homepage:
- Size: 2.57 MB
- Stars: 164
- Watchers: 26
- Forks: 20
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-star-libs - midwayjs / injection
README
# Injection
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/midwayjs/injection/blob/master/LICENSE)
[![GitHub tag](https://img.shields.io/github/tag/midwayjs/injection.svg)]()
[![Build Status](https://travis-ci.org/midwayjs/injection.svg?branch=master)](https://travis-ci.org/midwayjs/injection)
[![Test Coverage](https://img.shields.io/codecov/c/github/midwayjs/injection/master.svg)](https://codecov.io/gh/midwayjs/injection/branch/master)
[![Package Quality](http://npm.packagequality.com/shield/injection.svg)](http://packagequality.com/#?package=injection)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/midwayjs/midway/pulls)Injection is a powerful inversion of control container that is widely used in the midway framework and brings good user experience.
## Installation
```bash
$ npm install injection reflect-metadata --save
```Node.js >= 10.0.0 required.
> Injection requires TypeScript >= 2.0 and the experimentalDecorators, emitDecoratorMetadata, types and lib compilation options in your tsconfig.json file.
```json
{
"compilerOptions": {
"target": "ES2018",
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"inlineSourceMap":true,
"noImplicitThis": true,
"noUnusedLocals": true,
"stripInternal": true,
"pretty": true,
"declaration": true,
"outDir": "dist",
"lib": ["ES2018", "dom"]
}
}
```## Getting Started
```ts
import {Container, provide, inject} from 'injection';@provide('userModel')
class UserModel {}
@provide('userService')
class UserService {
@inject()
private userModel;
async getUser(uid) {
// TODO
return 'Alex';
}
}const container = new Container();
container.bind(UserService);
container.bind(UserModel);async function getData() {
const userService = await container.getAsync('userService');
const data = await userService.getUser(123);
return data;
}getData().then(console.log);
// Alex
```Document: [https://midwayjs.org/injection/guide.html](https://midwayjs.org/injection/guide.html)
## License
[MIT]((http://github.com/midwayjs/midway/blob/master/LICENSE))