{"id":16328952,"url":"https://github.com/TypeFox/djinject","last_synced_at":"2025-10-25T21:30:47.705Z","repository":{"id":62074661,"uuid":"537368728","full_name":"TypeFox/djinject","owner":"TypeFox","description":"Dependency injection done right.","archived":false,"fork":false,"pushed_at":"2023-03-23T00:10:22.000Z","size":250,"stargazers_count":24,"open_issues_count":6,"forks_count":1,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-02-08T04:45:39.317Z","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/TypeFox.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,"governance":null},"funding":{"github":"TypeFox"}},"created_at":"2022-09-16T08:21:22.000Z","updated_at":"2024-11-04T06:18:41.000Z","dependencies_parsed_at":"2023-11-13T07:44:07.228Z","dependency_job_id":null,"html_url":"https://github.com/TypeFox/djinject","commit_stats":null,"previous_names":["langium/ginject","langium/djinject"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TypeFox%2Fdjinject","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TypeFox%2Fdjinject/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TypeFox%2Fdjinject/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TypeFox%2Fdjinject/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TypeFox","download_url":"https://codeload.github.com/TypeFox/djinject/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238212350,"owners_count":19434946,"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-10T23:14:36.718Z","updated_at":"2025-10-25T21:30:47.229Z","avatar_url":"https://github.com/TypeFox.png","language":"TypeScript","readme":"\u003cdiv id=\"djinject-logo\" align=\"center\"\u003e\n  \u003ca href=\"https://github.com/langium/djinject\"\u003e\n    \u003cimg alt=\"Djinject Logo\" width=\"450\" src=\"https://user-images.githubusercontent.com/743833/197622552-e613a4cc-bfd7-4757-b8ef-7679249e109f.png\"\u003e\n  \u003c/a\u003e\n  \u003ch3\u003e\n    Dependency injection done right.\n  \u003c/h3\u003e\n\u003c/div\u003e\n\n\u003cdiv id=\"badges\" align=\"center\"\u003e\n\n[![npm version](https://img.shields.io/npm/v/djinject?logo=npm\u0026style=flat-square)](https://www.npmjs.com/package/djinject/)\n[![build](https://img.shields.io/github/actions/workflow/status/langium/djinject/build.yml?branch=main\u0026logo=github\u0026style=flat-square)](https://github.com/langium/djinject/actions/workflows/build.yml)\n[![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod\u0026style=flat-square)](https://gitpod.io/#https://github.com/langium/djinject)\n\n\u003c/div\u003e\n\n\u003cbr\u003e\n\n**Djinject** empowers developers designing decoupled applications and frameworks. **Djinject**'s main goal is increasing the developer experience by offering a tiny, yet powerful API, keeping dependencies in central module definitions and by using TypeScript's type system to restrain runtime challenges.\n\n\u003cdiv id=\"djinject vs inversify\" align=\"center\"\u003e\n\n|                  |   djinject  |  inversify  |\n|------------------|:----------:|:-----------:|\n| minified         | [![minified size](https://img.shields.io/bundlephobia/min/djinject?label=\u0026style=flat-square)](https://bundlephobia.com/result?p=djinject@latest) | [![minified size](https://img.shields.io/bundlephobia/min/inversify?label=\u0026style=flat-square)](https://bundlephobia.com/result?p=inversify@latest) |\n| minzipped        | [![minzipped size](https://img.shields.io/bundlephobia/minzip/djinject?label=\u0026style=flat-square)](https://bundlephobia.com/result?p=djinject@latest) | [![minzipped size](https://img.shields.io/bundlephobia/minzip/inversify?label=\u0026style=flat-square)](https://bundlephobia.com/result?p=inversify@latest) |\n| typesafe         |      ✅    |      ❌      |\n| requirements     |    none    | decorators  |\n| style            | functional | imperative  |\n| API surface area |    tiny    | non-trivial |\n\n\u003c/div\u003e\n\n## Features\n\n* type-safe\n* tiny footprint\n* property injection\n* rebinding dependencies\n* dependency cycle detection\n* lazy and eager initialization\n* no magic, no global state\n* no decorators\n* no dependencies\n* no configuration\n* no boilerplate\n\n## Quickstart\n\nThe first step is to add **djinject** to your application.\n\n```sh\nnpm i djinject\n```\n\nBascially, the only thing needed is to define **modules** of **factories** and finally call **inject**. The resulting **container** provides concrete **instances**.\n\n```ts\nimport { inject } from 'djinject';\n\n// create an inversion of control container\nconst container = inject({\n    hi: () =\u003e 'Hi',\n    sayHi: () =\u003e (name: string) =\u003e `${container.hi} ${name}!`\n});\n\n// prints 'Hi Djinject!'\nconsole.log(container.sayHi('Djinject'));\n```\n\n## API\n\n### Terminology\n\nThe **inject** function is turning **modules** into a **container**. A **module** is a plain vanilla JS object, composed of nested **groups** and **dependency factories**. Factories may return any JS value, e.g. constants, singletons and providers. Unlike [Inversify](https://github.com/inversify/InversifyJS), there is no need to decorate classes.\n\n```ts\nimport { inject, Module } from 'djinject';\n\n// Defining a _context_ of dependencies\ntype Context = {\n    group: {\n        value: Value // any JS type, here a class\n    }\n}\n\n// A _module_ contains nested _groups_ (optional) and _factories_\nconst module = {\n    group: {\n        // a factory of type Factory\u003cContext, Value\u003e\n        value: (ctx: Context) =\u003e new Value(ctx)\n    }\n} satisfies Module\u003cContext\u003e;\n\n// A _container_ of type Container\u003c[Module\u003cContext\u003e]\u003e = Context\nconst container = inject(module);\n\n// Values can be obtained from the container\nconst value = container.group.value;\n```\n\n### Context\n\nA **container** provides each **factory** with a parameter called **context**.\n\n```ts\ntype C = {\n    value: string\n}\n\n// ❌ compiler error: value is missing\nconst container = inject({\n    factory: (ctx: C) =\u003e () =\u003e ctx.value\n});\n```\n\nThe **context** of type **C** provides a **value** that can't be resolved. The **inject** call is type-checked by TS the way that the completeness of the arguments is checked.\n\nSuch **missing dependencies** need to be provided by adding additional **modules** to the **inject** call.\n\n```ts\n// ✅ fixed, value is defined\nconst container = inject({\n    createValue: (ctx: C) =\u003e () =\u003e ctx.value\n}, {\n    value: () =\u003e '🧞‍♀️'\n});\n```\n\nNow the compiler is satisfied and we can start using the **container**.\n\n```ts\n// prints 🧞‍♀️\nconsole.log(container.createValue());\n```\n\nYou might have noticed that the **container** automatically **injects** itself as the **context** when calling the **createValue** function.\n\n### Eager vs lazy initialization\n\nA dependency **container.group.value** is **lazily** initialized when first accessed on the container. Initialize a factory **eagerly** at the time of the **inject** call by wrapping it in an **init** call. Hint: groups can be eagerly initialized as well.\n\nA use case for **eager initialization** would be to ensure that **side effects** take place during the initialization of the **container**.\n\n```ts\nimport { init, inject, Module } from 'djinject';\n\ntype C = {\n    logger: string\n}\n\nconst module = {\n    service: init(() =\u003e {\n        console.log('Service initialized');\n    })\n} satisfies Module\u003cC\u003e;\n\nconst ctr = inject(module);\n\nconsole.log('App started');\n\nctr.service\n```\n\nIn the **eager** case, the output is\n\n```plain\nService initialized\nApp started\n```\n\nIn the **lazy** case, the output is\n\n```plain\nApp started\nService initialized\n```\n\nPlease note that **eager factories** overwrite **lazy factories** vice versa when **rebinding** them using **additional modules** in the **inject** call.\n\n### Rebinding dependencies\n\nThe main advantage of **dependency injection** arises from the fact that an application is able to **rebind dependencies**. That way the **structure** of a system can be fixated while the **behavior** can be changed.\n\nThe main vehicle for **rebinding dependencies** is the **inject** function which receives a variable amount of **modules**.\n\nThe behavior of an application can be enhanced by overwriting existing functionality using additional modules.\n\n```ts\ntype C = {\n    test: () =\u003e void\n    eval: (a: number, b: number) =\u003e number\n}\n\nconst m1 = {\n    test: (ctx) =\u003e () =\u003e {\n        console.log(ctx.eval(1, 1));\n    },\n    eval: () =\u003e (a, b) =\u003e a + b\n} satisfies Module\u003cC, C\u003e; // requires C\n\nconst m2 = {\n    eval: () =\u003e (a, b) =\u003e a * b\n} satisfies Module\u003cC\u003e; // partial C\n\nconst ctr = inject(m1, m2);\n\n// = 1\nctr.test();\n```\n","funding_links":["https://github.com/sponsors/TypeFox"],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTypeFox%2Fdjinject","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTypeFox%2Fdjinject","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTypeFox%2Fdjinject/lists"}