{"id":18825007,"url":"https://github.com/lulusir/di","last_synced_at":"2025-04-14T01:31:19.978Z","repository":{"id":81888849,"uuid":"467116832","full_name":"lulusir/di","owner":"lulusir","description":"A  simple Dependency Injection","archived":false,"fork":false,"pushed_at":"2023-03-10T03:30:12.000Z","size":550,"stargazers_count":7,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-27T15:48:54.746Z","etag":null,"topics":["dependency-injection","injection","ioc","ioc-container","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lulusir.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2022-03-07T13:59:19.000Z","updated_at":"2024-06-07T22:11:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"d21a74dd-a333-4dbc-b23e-86d27b215e19","html_url":"https://github.com/lulusir/di","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lulusir%2Fdi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lulusir%2Fdi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lulusir%2Fdi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lulusir%2Fdi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lulusir","download_url":"https://codeload.github.com/lulusir/di/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248807537,"owners_count":21164701,"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":["dependency-injection","injection","ioc","ioc-container","typescript"],"created_at":"2024-11-08T00:58:09.769Z","updated_at":"2025-04-14T01:31:19.423Z","avatar_url":"https://github.com/lulusir.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"The interface refers to [TSyringe](https://github.com/Microsoft/tsyringe)\n\n## install\n```bash\nnpm install @lujs/di\n```\n\n### api\n#### injectable()\nThe injectable decorator marks a class as injectable, meaning that its dependencies can be resolved and injected by the Container.\n```typescript\nimport { injectable, container } from './injectable';\nclass B {\n  name = 'b'\n}\n\n@injectable()\nclass A {\n  constructor(b: B) {}\n}\n\n\nconst a = container.resolve(A)\nconsole.log(a.b) // 'b'\n```\n\n#### singleton()\nThe singleton decorator marks a class as a singleton, meaning that the same instance will be used every time \n```typescript\nimport { singleton, container } from './injectable';\nclass B {\n  name = 'b'\n}\n\n@singleton()\nclass A {\n  constructor(b: B) {}\n}\n\nconst a1 = container.resolve(A)\nconst a2 = container.resolve(A)\n\nconsole.log(a1 === a2) // true\n```\n#### inject()\nThe inject decorator marks a constructor parameter as a dependency to be resolved and injected by the Container.\n```typescript\nimport { inject, InjectToken } from './injectable';\n\nconst token = Symbol('token');\ninterface B {}\n\n@injectable()\nclass A {\n  constructor(@inject(token) public b: B) {}\n\n  _() {\n    console.log(this.b);\n  }\n}\n\nclass B1 implements B {}\n\ncontainer.register(token, { useClass: B1 });\n\nconst a = container.resolve(A);\n\nexpect(a.b).toBeDefined();\nexpect(a.b).toBeInstanceOf(B1);\n```\n### circular dependency\nUse proxy objects to solve circular dependency problems. But you should approach this from the perspective of code design\n```typescript\n@injectable()\nexport class CircleA {\n  constructor(@inject(delay(() =\u003e CircleB)) public b: CircleB) {}\n}\n\n@injectable()\nexport class CircleB {\n  constructor(@inject(delay(() =\u003e CircleA)) public a: CircleA) {}\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flulusir%2Fdi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flulusir%2Fdi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flulusir%2Fdi/lists"}