{"id":13835028,"url":"https://github.com/midwayjs/injection","last_synced_at":"2025-04-07T12:05:27.523Z","repository":{"id":45242156,"uuid":"166758008","full_name":"midwayjs/injection","owner":"midwayjs","description":"Injection is a powerful inversion of control container that is widely used in the midway framework and brings good user experience.","archived":false,"fork":false,"pushed_at":"2021-12-28T16:29:32.000Z","size":2691,"stargazers_count":164,"open_issues_count":4,"forks_count":20,"subscribers_count":26,"default_branch":"master","last_synced_at":"2024-04-23T22:09:42.646Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/midwayjs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-21T06:12:05.000Z","updated_at":"2023-09-28T10:57:44.000Z","dependencies_parsed_at":"2022-08-28T13:20:32.022Z","dependency_job_id":null,"html_url":"https://github.com/midwayjs/injection","commit_stats":null,"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/midwayjs%2Finjection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/midwayjs%2Finjection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/midwayjs%2Finjection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/midwayjs%2Finjection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/midwayjs","download_url":"https://codeload.github.com/midwayjs/injection/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247648976,"owners_count":20972945,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T14:00:55.212Z","updated_at":"2025-04-07T12:05:27.504Z","avatar_url":"https://github.com/midwayjs.png","language":"TypeScript","funding_links":[],"categories":["目录"],"sub_categories":[],"readme":"# Injection\n\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/midwayjs/injection/blob/master/LICENSE)\n[![GitHub tag](https://img.shields.io/github/tag/midwayjs/injection.svg)]()\n[![Build Status](https://travis-ci.org/midwayjs/injection.svg?branch=master)](https://travis-ci.org/midwayjs/injection)\n[![Test Coverage](https://img.shields.io/codecov/c/github/midwayjs/injection/master.svg)](https://codecov.io/gh/midwayjs/injection/branch/master)\n[![Package Quality](http://npm.packagequality.com/shield/injection.svg)](http://packagequality.com/#?package=injection)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/midwayjs/midway/pulls)\n\nInjection is a powerful inversion of control container that is widely used in the midway framework and brings good user experience. \n\n## Installation\n\n```bash\n$ npm install injection reflect-metadata --save\n```\n\nNode.js \u003e= 10.0.0 required.\n\n\u003e Injection requires TypeScript \u003e= 2.0 and the experimentalDecorators, emitDecoratorMetadata, types and lib compilation options in your tsconfig.json file.\n\n```json\n{\n  \"compilerOptions\": {\n    \"target\": \"ES2018\",\n    \"module\": \"commonjs\",\n    \"moduleResolution\": \"node\",\n    \"experimentalDecorators\": true,\n    \"emitDecoratorMetadata\": true,\n    \"inlineSourceMap\":true,\n    \"noImplicitThis\": true,\n    \"noUnusedLocals\": true,\n    \"stripInternal\": true,\n    \"pretty\": true,\n    \"declaration\": true,\n    \"outDir\": \"dist\",\n    \"lib\": [\"ES2018\", \"dom\"]\n  }\n}\n```\n\n## Getting Started\n\n```ts\nimport {Container, provide, inject} from 'injection';\n\n@provide('userModel')\nclass UserModel {\n\n}\n\n@provide('userService')\nclass UserService {\n  \n  @inject()\n  private userModel;\n  \n  async getUser(uid) {\n    // TODO\n    return 'Alex';\n  }\n}\n\n\nconst container = new Container();\ncontainer.bind(UserService);\ncontainer.bind(UserModel);\n\nasync function getData() {\n  const userService = await container.getAsync\u003cUserService\u003e('userService'); \n  const data = await userService.getUser(123);\n  return data;\n}\n\ngetData().then(console.log);\n// Alex\n```\n\nDocument: [https://midwayjs.org/injection/guide.html](https://midwayjs.org/injection/guide.html)\n\n## License\n\n[MIT]((http://github.com/midwayjs/midway/blob/master/LICENSE))\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmidwayjs%2Finjection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmidwayjs%2Finjection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmidwayjs%2Finjection/lists"}