{"id":13526751,"url":"https://github.com/zhang740/power-di","last_synced_at":"2025-05-13T12:52:37.810Z","repository":{"id":48861650,"uuid":"85177265","full_name":"zhang740/power-di","owner":"zhang740","description":"A lightweight Dependency Injection library.","archived":false,"fork":false,"pushed_at":"2025-01-21T05:35:35.000Z","size":283,"stargazers_count":178,"open_issues_count":2,"forks_count":9,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-27T05:19:12.452Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/zhang740.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-16T09:26:50.000Z","updated_at":"2025-04-24T11:38:07.000Z","dependencies_parsed_at":"2024-01-13T22:59:12.219Z","dependency_job_id":"9662877d-4199-47f5-a977-9157e4c4a0d1","html_url":"https://github.com/zhang740/power-di","commit_stats":{"total_commits":225,"total_committers":5,"mean_commits":45.0,"dds":"0.12444444444444447","last_synced_commit":"f452eeffa8edd2531a70b90df20a9a017ccb46be"},"previous_names":[],"tags_count":111,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhang740%2Fpower-di","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhang740%2Fpower-di/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhang740%2Fpower-di/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhang740%2Fpower-di/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zhang740","download_url":"https://codeload.github.com/zhang740/power-di/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253947792,"owners_count":21988945,"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-01T06:01:34.196Z","updated_at":"2025-05-13T12:52:37.778Z","avatar_url":"https://github.com/zhang740.png","language":"TypeScript","funding_links":[],"categories":["Repository"],"sub_categories":["Inversion of control / Dependency Injection (Ioc/DI)"],"readme":"# Power DI\n\n[![CI](https://img.shields.io/travis/zhang740/power-di.svg?style=flat-square)](https://travis-ci.org/zhang740/power-di)\n[![Coverage](https://img.shields.io/coveralls/zhang740/power-di.svg?style=flat-square)](https://coveralls.io/github/zhang740/power-di)\n[![Version](https://img.shields.io/npm/v/power-di.svg?style=flat-square)](https://www.npmjs.com/package/power-di)\n[![License](https://img.shields.io/npm/l/power-di.svg?style=flat-square)](https://github.com/zhang740/power-di/blob/master/LICENSE)\n\nA lightweight Dependency Injection library. Using es6 and other features, remove unnecessary concepts, easy and convenient to use.\n\n## Install\n```shell\nnpm i power-di --save\n```\n\n## Example\n\n### a simple example\n```ts\nimport { IocContext } from 'power-di';\n\nclass AService { }\n\n// default instance, you can also new IocContext to get a instance.\nconst context = IocContext.DefaultInstance;\ncontext.register(AService);\nconst aService = context.get(AService); // a instance of AService\n```\n\n### inject with key\n```ts\nclass AService { }\n\nconst context = IocContext.DefaultInstance;\ncontext.register(AService, 'XService'); // key need a string or class, e.g super class or whatever class.\ncontext.get('XService');\n```\n\n### use with decorators\n```ts\nconst context = new IocContext();\n\n@injectable()\nclass NRService { }\n\n@injectable()\nclass LITestService {\n  @inject()\n  public testService: NRService;\n}\n\nconst test = context.get(LITestService);\n```\n\n### collect impl or extends of some interface/base class\n```ts\nclass A { }\n\n@classInfo()\nclass B extends A { }\n\n@classInfo()\nclass C extends A { }\n\n@injectable()\nclass LITestService {\n  @imports({ type: A })\n  public testService: A[];\n}\n\nconst test = context.get(LITestService); // test.testService as A[];\n```\n\n### use in react\n```tsx\nconst context = new IocContext;\nclass NRService { }\ncontext.register(NRService);\n\nclass TestComponent extends Component\u003c{}, {}\u003e {\n  @inject()\n  service: NRService;\n\n  componentDidMount() {\n    t.true(this.service instanceof NRService);\n  }\n\n  render(): any {\n    return null;\n  }\n}\n\ncreate(\n  \u003cIocProvider context={context}\u003e\n    \u003cTestComponent /\u003e\n  \u003c/IocProvider\u003e\n);\n```\n\n## Class Loader\nPower DI introduced the concept of class loader. We can customize the find rule of ioc.\n\n#### [See the test case for details.](https://github.com/zhang740/power-di/tree/master/test)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhang740%2Fpower-di","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzhang740%2Fpower-di","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhang740%2Fpower-di/lists"}