{"id":17255564,"url":"https://github.com/ffmathy/fluffyspoon.javascript.inversionofcontrol","last_synced_at":"2025-04-14T05:42:08.514Z","repository":{"id":33117932,"uuid":"145879554","full_name":"ffMathy/FluffySpoon.JavaScript.InversionOfControl","owner":"ffMathy","description":null,"archived":false,"fork":false,"pushed_at":"2022-04-09T15:07:08.000Z","size":640,"stargazers_count":4,"open_issues_count":4,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-11T06:12:26.025Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ffMathy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"ffMathy","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2018-08-23T16:25:08.000Z","updated_at":"2025-03-02T07:10:41.000Z","dependencies_parsed_at":"2022-07-27T19:48:55.774Z","dependency_job_id":null,"html_url":"https://github.com/ffMathy/FluffySpoon.JavaScript.InversionOfControl","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/ffMathy%2FFluffySpoon.JavaScript.InversionOfControl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ffMathy%2FFluffySpoon.JavaScript.InversionOfControl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ffMathy%2FFluffySpoon.JavaScript.InversionOfControl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ffMathy%2FFluffySpoon.JavaScript.InversionOfControl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ffMathy","download_url":"https://codeload.github.com/ffMathy/FluffySpoon.JavaScript.InversionOfControl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248830419,"owners_count":21168272,"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-15T07:12:03.262Z","updated_at":"2025-04-14T05:42:08.491Z","avatar_url":"https://github.com/ffMathy.png","language":"TypeScript","funding_links":["https://github.com/sponsors/ffMathy"],"categories":[],"sub_categories":[],"readme":"`@fluffy-spoon/inverse` is an Inversion of Control framework.\r\n\r\n# Installing\r\n`npm i @fluffy-spoon/inverse --save`\r\n\r\n# TypeScript requirements\r\nThis framework requires the following values set in your TypeScript configuration (`tsconfig.json`):\r\n\r\n```json\r\n{\r\n    \"compilerOptions\": {\r\n        \"experimentalDecorators\": true,\r\n        \"emitDecoratorMetadata\": true,\r\n        \"esModuleInterop\": true\r\n    }\r\n}\r\n```\r\n\r\n# Examples\r\nThese examples assume that there's a `Bar` class which as some dependencies (`A1` and `A2`), which then further have some dependencies (`B1` and `B2`) as shown below.\r\n\r\n_Note how every class has an `Injectable` decorator, and how every dependency has an `Inject` decorator. That's all that is needed! The examples below would work for exported classes from separate files too._\r\n\r\n```typescript\r\n@Injectable\r\nclass Bar {\r\n    constructor(\r\n        @Inject private a: A1, \r\n        @Inject private b: A2, \r\n        @Inject private c: A2) \r\n    {\r\n    }\r\n}\r\n\r\n@Injectable\r\nclass B1 {\r\n\r\n}\r\n\r\n@Injectable\r\nclass B2 implements B1 {\r\n\r\n}\r\n\r\n@Injectable\r\nclass A1 {\r\n    constructor(\r\n        @Inject private a: B1) \r\n    {\r\n    }\r\n}\r\n\r\n@Injectable\r\nclass A2 {\r\n    constructor(\r\n        @Inject private a: B2) \r\n    {\r\n    }\r\n}\r\n```\r\n\r\n## Basic use\r\nIn the following example, a `Bar` instance is requested from the IOC container. Per default, each dependency will be instantiated recursively with its own constructor.\r\n\r\nThe following code:\r\n\r\n```typescript\r\nimport Container from '@fluffy-spoon/inverse';\r\n\r\nconst container = new Container();\r\n\r\nconst bar = container.resolve(Bar);\r\n```\r\n\r\nIs equal to:\r\n\r\n```typescript\r\nconst bar = new Bar(\r\n    new A1(new B1()),\r\n    new A2(new B2()),\r\n    new A2(new B2()));\r\n```\r\n\r\n## Changing dependencies\r\nIn the below example, we change all `B1` to be instances of `B2` instead. This is possible because `B2` implements `B1`.\r\n\r\nThe following code:\r\n\r\n```typescript\r\nimport Container from '@fluffy-spoon/inverse';\r\n\r\nconst container = new Container();\r\ncontainer.whenRequestingType(B1).useType(B2);\r\n\r\nconst bar = container.resolve(Bar);\r\n```\r\n\r\nIs equal to:\r\n\r\n```typescript\r\nconst bar = new Bar(\r\n    new A1(new B2()),\r\n    new A2(new B2()),\r\n    new A2(new B2()));\r\n```\r\n\r\nYou can also use:\r\n- `useFactory` for determining how an instance should be created.\r\n- `useRequestedType` to use the requested type (this is default).\r\n\r\n## Changing lifetime\r\nIn the below example, we make `B2` instances be single-instance by using the `asSingleInstance` method.\r\n\r\n_Note that `useRequestedType` is called here. This just means that when a `B2` instance is requested, a `B2` instance is also injected (which is default)._\r\n\r\n```typescript\r\nimport { Container } from '@fluffy-spoon/inverse';\r\n\r\nconst container = new Container();\r\ncontainer.whenRequestingType(B2).useRequestedType().asSingleInstance();\r\n\r\nconst bar = container.resolve(Bar);\r\n```\r\n\r\nIs equal to:\r\n\r\n```typescript\r\nconst b2 = new B2();\r\nconst bar = new Bar(\r\n    new A1(new B1()),\r\n    new A2(b2,\r\n    new A2(b2)));\r\n```\r\n\r\nYou can also use:\r\n- `asInstancePerRequest` to create a new instance from the type or factory provided every time (this is default).\r\n\r\n# Framework support\r\n`@fluffy-spoon/inverse` can be used without a framework, but more packages can be used if you want to tightly integrate it.\r\n\r\n- **Vue.js:** [`@fluffy-spoon/inverse-vue`](https://www.npmjs.com/package/@fluffy-spoon/inverse-vue)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fffmathy%2Ffluffyspoon.javascript.inversionofcontrol","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fffmathy%2Ffluffyspoon.javascript.inversionofcontrol","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fffmathy%2Ffluffyspoon.javascript.inversionofcontrol/lists"}