{"id":13990873,"url":"https://github.com/typesoft/container-ioc","last_synced_at":"2026-02-12T01:42:39.574Z","repository":{"id":57150419,"uuid":"102655731","full_name":"typesoft/container-ioc","owner":"typesoft","description":"Inversion of Control  container \u0026 Dependency Injection for Javascript and Node.js apps powered by Typescript.","archived":false,"fork":false,"pushed_at":"2020-01-25T17:08:09.000Z","size":145,"stargazers_count":102,"open_issues_count":12,"forks_count":25,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-11-22T23:05:00.404Z","etag":null,"topics":["container","dependency","dependency-injection","dependency-manager","di","es2015","es2016","es2017","es6","factory","inject","injection","injector","inversion-of-control","ioc","lifetime","service-locator","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/container-ioc","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/typesoft.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-06T20:35:01.000Z","updated_at":"2024-09-26T19:51:52.000Z","dependencies_parsed_at":"2022-09-03T16:50:48.897Z","dependency_job_id":null,"html_url":"https://github.com/typesoft/container-ioc","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typesoft%2Fcontainer-ioc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typesoft%2Fcontainer-ioc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typesoft%2Fcontainer-ioc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typesoft%2Fcontainer-ioc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/typesoft","download_url":"https://codeload.github.com/typesoft/container-ioc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227099040,"owners_count":17730698,"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":["container","dependency","dependency-injection","dependency-manager","di","es2015","es2016","es2017","es6","factory","inject","injection","injector","inversion-of-control","ioc","lifetime","service-locator","typescript"],"created_at":"2024-08-09T13:03:26.514Z","updated_at":"2026-02-12T01:42:34.533Z","avatar_url":"https://github.com/typesoft.png","language":"TypeScript","readme":"![alt text](http://abcselfstorageperth.com.au/wp-content/uploads/2014/08/icon-container-storage1.png)\n\n\n## **container-ioc** \nis a [Dependency Injection](https://en.wikipedia.org/wiki/Dependency_injection) / [Inversion of Control (IoC) container](http://martinfowler.com/articles/injection.html) package for Javascript and Node.js applications powered by Typescript . It manages the dependencies between classes, so that applications stay easy to change and maintain as they grow.\n\n[![npm version](https://badge.fury.io/js/container-ioc.svg)](https://badge.fury.io/js/container-ioc)\n[![Build Status](https://travis-ci.org/typesoft/container-ioc.svg?branch=master)](https://travis-ci.org/typesoft/container-ioc)\n[![npm](https://img.shields.io/npm/dt/container-ioc.svg)](https://www.npmjs.com/package/container-ioc)\n[![Gitter chat](https://badges.gitter.im/typesoft-org/container-ioc.png)](https://gitter.im/typesoft-org/container-ioc)\n[![license](https://img.shields.io/github/license/thohoh/container-ioc.svg)](https://github.com/thohoh/container-ioc/blob/master/LICENSE)\n\n### Features:\n* Well-known Angular DI API.\n* No external dependencies.\n* [Life Time control](#life-time-control).\n* [Hierarchical containers](#hierarchical-containers).\n* Resolves values using Classes, [Factories](#using-factories) and [Values](#using-values).\n* Descriptive error messages.\n* 97% test coverage.\n\n### Examples:  \n* [examples/javascript](examples/javascript)\n* [examples/typescript](examples/typescript)\n\n\n### Installation:\n```\nnpm install --save container-ioc\n```\n### Basics:\n\u003e Code examples below are written in Typescript. Check [examples/javascript](examples/javascript) for examples written in Javascript.\n\n#### Step 1. Define your interfaces and types.\n\u003e Possible values for types: **Symbol**, **string**, **Object**.\n\n```typescript\ninterface IApplication {\n    run(): void;\n}\n\ninterface IService {\n    serve(): void;\n}\n\nconst TApplication = Symbol('IApplication');\n\nconst TService = Symbol('IService');\n```\n\n#### Step 2. Declare dependencies with decorators **Injectable** and **Inject**.\n\n```typescript\nimport { Injectable, Inject } from 'container-ioc';\n\n@Injectable()\nexport class Application implements IApplication {\n    constructor(@Inject(TService) private service: IService) {}\n    \n    run(): void {\n        this.service.serve();\n    }\n}\n\n@Injectable()\nexport class Service implements IService {\n    serve(): void {\n        // serves\n    }\n}\n```\n\n#### Step 3. Create a container and register types in there.\n\n```typescript\nimport { Container } from 'container-ioc';\n\nlet container = new Container();\n\ncontainer.register([\n    { token: TApplication, useClass: Application },\n    { token: TService, useClass: Service }\n]);\n```\n\n#### Step 4. Resolve value from the container.\n\n```typescript\nlet app = container.resolve(TApplication);\n\napp.run();\n```\n\n#### Step 2 for Javascript.\n\u003e Since Javascript does not support parameter decorators, use alternative API for declaring dependencies. In this case we don't use **Inject** decorator. See [examples/javascript](examples/javascript) for more.\n```javascript\n\n@Injectable([TService])\nclass Service {\n    constructor(service) {\n        this.service = service;\n    }\n}\n```\n\n### Life Time control\n\u003e By default, containers resolve singletons when using **useClass** and **useFactory**.\nDefault life time for all items in a container can be set by passing an option object to it's contructor with **defailtLifeTime** attribute. Possible values: **LifeTime.PerRequest** (resolves instances) and **LifeTime.Persistent** (resolves singletons);\n\n```typescript\nimport { LifeTime } from 'container-ioc';\n\nconst container = new Container({\n    defaultLifeTime: LifeTime.PerRequest\n});\n```\n\u003e You can also specify life time individually for each item in a container by specifying **lifeTime** attribute.\n\n```typescript\ncontainer.register([\n    {\n        token: TService,\n        useClass: Service,\n        lifeTime: LifeTime.PerRequest\n    }\n]);\n```\n```typescript\ncontainer.register([\n    {\n        token: TService,\n        useFactory: () =\u003e {\n            return {\n                serve(): void {}\n            }\n        },\n        lifeTime: LifeTime.Persistent\n    }\n]);\n```\n\n### Hierarchical containers\n\u003e If a container can't find a value within itself, it will look it up in ascendant containers. There a 3 ways to set a parent for a container.\n\n###### 1. Container.createChild() method.\n```typescript\nconst parentContainer = new Container();\nconst childContainer = parentContainer.createChild();\n```\n\n###### 2. Container.setParent() method.\n```typescript\nconst parent = new Container();\nconst child = new Container();\n\nchild.setParent(parent);\n```\n\n###### 3. Via Container's constructor with options.\n```typescript\nconst parent = new Container();\nconst child = new Container({\n    parent: parent\n});\n```\n\n### Using Factories\n```typescript\n/* Without injections */\ncontainer.register([\n    {\n        token: 'TokenForFactory',\n        useFactory: () =\u003e {\n            return 'any-value';\n        }\n    }\n]);\n\n/* With injections */\ncontainer.register([\n    { token: 'EnvProvider', useClass: EnvProvider },\n    {\n        token: 'TokenForFactory',\n        useFactory: (envProvider) =\u003e {\n            // do something\n            return 'something';\n        },\n        inject: ['EnvProvider']\n    }\n]);\n```\n\n### Using Values \n```typescript\ncontainer.register([\n    { token: 'IConfig', useValue: {}}\n]);\n```\n\n### Shortcut for Classes\n```typescript\ncontainer.register([\n    App\n]);\n```\nIs the same as:\n```typescript\ncontainer.register([\n    { token: App, useClass: App }\n]);\n```\n\n## Contribution\nBecome a contributor to this project. Feel free to submit an [issue](https://github.com/thohoh/container-ioc/issues) or a pull request.\n\nsee [CONTRIBUTION.md](CONTRIBUTION.md) for more information.\n\nPlease see also our [Code of Conduct](CODE_OF_CONDUCT.md).","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypesoft%2Fcontainer-ioc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftypesoft%2Fcontainer-ioc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypesoft%2Fcontainer-ioc/lists"}