From bcdbfc84e1b27637d88a9f30b66bc0388a88c4cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Pintos=20L=C3=B3pez?= Date: Fri, 3 Jan 2025 13:07:11 +0100 Subject: [PATCH 1/5] feat(inversify-code-examples): add DI hierarchy example --- .../src/examples/diHierachy.int.spec.ts | 9 +++++++++ .../src/examples/diHierarchy.ts | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/diHierachy.int.spec.ts create mode 100644 packages/docs/tools/inversify-code-examples/src/examples/diHierarchy.ts diff --git a/packages/docs/tools/inversify-code-examples/src/examples/diHierachy.int.spec.ts b/packages/docs/tools/inversify-code-examples/src/examples/diHierachy.int.spec.ts new file mode 100644 index 00000000..41f7c672 --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/diHierachy.int.spec.ts @@ -0,0 +1,9 @@ +import { describe, expect, it } from '@jest/globals'; + +import { Katana, katana } from './diHierarchy'; + +describe('DI Hierachy', () => { + it('should provide a Katana', () => { + expect(katana).toBeInstanceOf(Katana); + }); +}); diff --git a/packages/docs/tools/inversify-code-examples/src/examples/diHierarchy.ts b/packages/docs/tools/inversify-code-examples/src/examples/diHierarchy.ts new file mode 100644 index 00000000..7282e994 --- /dev/null +++ b/packages/docs/tools/inversify-code-examples/src/examples/diHierarchy.ts @@ -0,0 +1,16 @@ +import { Container } from 'inversify'; + +const weaponIdentifier: symbol = Symbol.for('Weapon'); + +// Begin-example +class Katana {} + +const parentContainer: Container = new Container(); +parentContainer.bind(weaponIdentifier).to(Katana); + +const childContainer: Container = parentContainer.createChild(); + +const katana: Katana = childContainer.get(weaponIdentifier); +// End-example + +export { Katana, katana }; From 530cc5bb484016bcdf94b1478d8b859e5a69a18e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Pintos=20L=C3=B3pez?= Date: Fri, 3 Jan 2025 13:07:54 +0100 Subject: [PATCH 2/5] feat(inversify-site): add DI hierarchy page --- .../docs/fundamentals/di-hierarchy.mdx | 21 +++++++++++++++++++ .../fundamentals/lifecycle/_category_.json | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 packages/docs/services/inversify-site/docs/fundamentals/di-hierarchy.mdx diff --git a/packages/docs/services/inversify-site/docs/fundamentals/di-hierarchy.mdx b/packages/docs/services/inversify-site/docs/fundamentals/di-hierarchy.mdx new file mode 100644 index 00000000..479a61ca --- /dev/null +++ b/packages/docs/services/inversify-site/docs/fundamentals/di-hierarchy.mdx @@ -0,0 +1,21 @@ +--- +sidebar_position: 2 +title: DI Hierarchy +--- +import diHierachySource from '@inversifyjs/code-examples/generated/examples/diHierachy.ts.txt'; +import CodeBlock from '@theme/CodeBlock'; + +# DI Hierarchy +Some applications use a hierarchical dependency injection (DI) system. +For example, Angular applications use its own [hierarchical DI system](https://angular.dev/guide/di/hierarchical-dependency-injection). + +In a hierarchical DI system, a container can have a parent container and multiple containers can be used in one application. The containers form a hierarchical structure. + +When a container at the bottom of the hierarchical structure requests a dependency, +the container tries to satisfy that dependency with it's own bindings. If the container +lacks bindings, it passes the request up to its parent container. If that container can't +satisfy the request, it passes it along to its parent container. The requests keep +bubbling up until we find an container that can handle the request or run out of container +ancestors. + +{diHierachySource} diff --git a/packages/docs/services/inversify-site/docs/fundamentals/lifecycle/_category_.json b/packages/docs/services/inversify-site/docs/fundamentals/lifecycle/_category_.json index aadd11af..f00f94b9 100644 --- a/packages/docs/services/inversify-site/docs/fundamentals/lifecycle/_category_.json +++ b/packages/docs/services/inversify-site/docs/fundamentals/lifecycle/_category_.json @@ -1,4 +1,4 @@ { "label": "Lifecycle", - "position": 2 + "position": 3 } From 38497a6013a0b0debab938195d3d46c781c622d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Pintos=20L=C3=B3pez?= Date: Fri, 3 Jan 2025 13:08:12 +0100 Subject: [PATCH 3/5] chore: update docs packages to be private --- packages/docs/services/inversify-site/package.json | 4 ++-- packages/docs/tools/inversify-code-examples/package.json | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/docs/services/inversify-site/package.json b/packages/docs/services/inversify-site/package.json index 9ab444bc..a6a9984d 100644 --- a/packages/docs/services/inversify-site/package.json +++ b/packages/docs/services/inversify-site/package.json @@ -45,6 +45,7 @@ }, "license": "MIT", "name": "@inversifyjs/inversify-docs-site", + "private": true, "publishConfig": { "access": "public" }, @@ -61,6 +62,5 @@ "lint": "eslint ./src ./docusaurus.config.ts ./sidebars.ts", "serve": "docusaurus serve", "typecheck": "tsc" - }, - "version": "1.0.0" + } } diff --git a/packages/docs/tools/inversify-code-examples/package.json b/packages/docs/tools/inversify-code-examples/package.json index 824fd736..da7e1c46 100644 --- a/packages/docs/tools/inversify-code-examples/package.json +++ b/packages/docs/tools/inversify-code-examples/package.json @@ -40,6 +40,7 @@ }, "license": "MIT", "name": "@inversifyjs/code-examples", + "private": true, "publishConfig": { "access": "public" }, @@ -61,6 +62,5 @@ "test:js:coverage": "pnpm run test:unit:js --coverage", "test:uncommitted": "pnpm run test --changedSince=HEAD", "test:unit:js": "pnpm run test:js --selectProjects Unit" - }, - "version": "1.0.0" + } } From 7c41ece1c9f9aae2aa750895cfadddbe696d022a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Pintos=20L=C3=B3pez?= Date: Fri, 3 Jan 2025 13:08:23 +0100 Subject: [PATCH 4/5] feat(inversify-site): add inheritance page --- .../docs/fundamentals/inheritance.mdx | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 packages/docs/services/inversify-site/docs/fundamentals/inheritance.mdx diff --git a/packages/docs/services/inversify-site/docs/fundamentals/inheritance.mdx b/packages/docs/services/inversify-site/docs/fundamentals/inheritance.mdx new file mode 100644 index 00000000..a8c0ad3a --- /dev/null +++ b/packages/docs/services/inversify-site/docs/fundamentals/inheritance.mdx @@ -0,0 +1,35 @@ +--- +sidebar_position: 4 +title: Inheritance +--- +# Inheritance + +Inheritance can be accomplished as long as constructor parameters are properly decorated. There're two ways to have constructor parameters properly decorated: + +- The number of decorated constructor arguments in a derived class is greater or equal than the number of constructor arguments in its base class. +- [skipBaseClassChecks](/docs/api/container#skipbaseclasschecks) option is enabled + +## Example of wrong inheritance injection + +```ts +@injectable() +class Warrior { + public rank: string; + constructor(rank: string) { // args count = 1 + this.rank = rank; + } +} + +@injectable() +class SamuraiMaster extends Warrior { + constructor() { // args count = 0 + super("master"); + } +} +``` + +When trying to get a `SamuraiMaster`, the container throws an Error alerting the constructor parameters are not properly decorated. + +## Using @unmanaged decorator + +The [unmanaged](/docs/api/decorator#unmanaged) decorator is a good way to tell inversify a base type constructor param is should not be managed. This is often the case when dealing with inheritance hierarchies in which only leaf types are injected. From ae3053316d214254eecf76695a888471d50c56ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Pintos=20L=C3=B3pez?= Date: Fri, 3 Jan 2025 13:12:54 +0100 Subject: [PATCH 5/5] fix(inversify-site): fix url typo --- .../inversify-site/docs/fundamentals/di-hierarchy.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/docs/services/inversify-site/docs/fundamentals/di-hierarchy.mdx b/packages/docs/services/inversify-site/docs/fundamentals/di-hierarchy.mdx index 479a61ca..3f022ec7 100644 --- a/packages/docs/services/inversify-site/docs/fundamentals/di-hierarchy.mdx +++ b/packages/docs/services/inversify-site/docs/fundamentals/di-hierarchy.mdx @@ -2,7 +2,7 @@ sidebar_position: 2 title: DI Hierarchy --- -import diHierachySource from '@inversifyjs/code-examples/generated/examples/diHierachy.ts.txt'; +import diHierarchySource from '@inversifyjs/code-examples/generated/examples/diHierarchy.ts.txt'; import CodeBlock from '@theme/CodeBlock'; # DI Hierarchy @@ -18,4 +18,4 @@ satisfy the request, it passes it along to its parent container. The requests ke bubbling up until we find an container that can handle the request or run out of container ancestors. -{diHierachySource} +{diHierarchySource}