All Projects → midwayjs → Injection

midwayjs / Injection

Licence: mit
Injection is a powerful inversion of control container that is widely used in the midway framework and brings good user experience.

Programming Languages

typescript
32286 projects

Injection

GitHub license GitHub tag Build Status Test Coverage Package Quality PRs Welcome

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

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].