injection
v1.8.6
Published
A New IoC Container For All Node.js Application
Downloads
790
Readme
Injection
Injection is a powerful inversion of control container that is widely used in the midway framework and brings good user experience.
Installation
$ 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.
{
"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
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>('userService');
const data = await userService.getUser(123);
return data;
}
getData().then(console.log);
// Alex
Document: https://midwayjs.org/injection/guide.html