{"id":16888079,"url":"https://github.com/otbe/di","last_synced_at":"2025-03-20T06:43:34.635Z","repository":{"id":57361043,"uuid":"91815702","full_name":"otbe/di","owner":"otbe","description":"Simple DI written for TypeScript","archived":false,"fork":false,"pushed_at":"2018-08-10T08:59:57.000Z","size":240,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-25T20:46:08.631Z","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/otbe.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2017-05-19T14:43:46.000Z","updated_at":"2022-11-20T16:42:01.000Z","dependencies_parsed_at":"2022-09-26T16:40:52.264Z","dependency_job_id":null,"html_url":"https://github.com/otbe/di","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/otbe%2Fdi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/otbe%2Fdi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/otbe%2Fdi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/otbe%2Fdi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/otbe","download_url":"https://codeload.github.com/otbe/di/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244566930,"owners_count":20473451,"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-10-13T16:48:47.359Z","updated_at":"2025-03-20T06:43:34.613Z","avatar_url":"https://github.com/otbe.png","language":"TypeScript","readme":"# simple-ts-di\n[![Build Status](https://travis-ci.org/otbe/di.svg?branch=master)](https://travis-ci.org/otbe/di)\n\nSimple DI framework written in and for TypeScript.\nBy default all bindings are in singleton scope. Where possible you can call a ```transient()``` binder function to create a transient binding.\n\n## Setup\nMake sure your ```tsconfig.json``` contains\n\n```json\n{\n  \"experimentalDecorators\": true,\n  \"emitDecoratorMetadata\": true\n}\n```\n\nInstall ```reflect-metadata``` and include it somewhere.\n\nMake sure your setup supports Maps and Symbols.\n\n## Example\n### Constructor injection\n```typescript\nclass Service {\n  sayHi() { return 'hi'; }\n}\n\n@inject()\nclass Test {\n  constructor(service: Service) {\n    service.sayHi();\n  }\n}\n\nclass MyModule implements Module {\n  init(bind: Bind) {\n    bind(Service).transient(); // transient scope\n    bind(Test); // singleton scope\n                // shortcut for bind(Test).to(Test)\n  }\n}\n\nconst container = new Container(new MyModule());\nconst test = container.get(Test);\n```\n\n### Property injection\n```simple-ts-di``` supports property injection. It will only work if the type (here ```Service```) has a meaningful runtime representation. So it only works for classes (as of now). If you want to inject some primitives or a service which implements an interface you have to use named injection. If your environment dont let you control how the class is instantiated (e.g. react components are instantiated by react and not via container.get(...)), you can use a special version of inject - abounded inject. See \"bounded property injection\".\n\n```typescript\nclass Service {\n  sayHi() { return 'hi'; }\n}\n\nclass Test {\n  @inject()\n  private service: Service;\n\n  sayHelloToService() {\n    return this.service.sayHi();\n  }\n}\n\nclass MyModule implements Module {\n  init(bind: Bind) {\n    bind(Service).transient(); // transient scope\n    bind(Test); // singleton scope\n                // shortcut for bind(Test).to(Test)\n  }\n}\n\nconst container = new Container(new MyModule());\nconst test = container.get(Test);\n```\n\n### named injection\n```typescript\nconst SERVICE = Symbol();\nconst PRIMITIVE = Symbol();\n\ninterface IService {\n  sayHi(): string;\n}\n\nclass Service implements IService {\n  sayHi() { return 'hi'; }\n}\n\n@inject([SERVICE, PRIMITIVE])\nclass Test {\n  @inject(SERVICE)\n  private service: IService;\n\n  @inject(PRIMITIVE)\n  private primitive: number;\n\n  constructor(service: IService, primitive: number) {\n  }\n}\n\nclass MyModule implements Module {\n  init(bind: Bind) {\n    bind(SERVICE).to(Service);\n    bind(PRIMITIVE).toValue(10);\n    bind(Test);\n  }\n}\n\nconst container = new Container(new MyModule());\nconst test = container.get(Test);\n```\n\n### bounded property injection\n```typescript\nclass Service {\n  sayHi() {\n    return 'hi';\n  }\n}\n\nclass MyModule implements Module {\n  init(bind: Bind) {\n    bind(Service);\n  }\n}\n\nconst container = new Container(new MyModule());\nconst bInject = createBoundedInject(container);\n\nclass Test {\n  @bInject() service: Service;\n}\n\nconst t = new Test();\n\nt.service.sayHi(); // returns Hi\n```\n\nSee ```tests/``` for more complex examples and API.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fotbe%2Fdi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fotbe%2Fdi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fotbe%2Fdi/lists"}