From e0472c8fd6873f05b92f93f892d535f3c5f63f9d Mon Sep 17 00:00:00 2001 From: Bernat Felip Date: Mon, 26 Apr 2021 17:50:14 +0200 Subject: [PATCH 1/3] Implemented change and some tests --- src/__tests__/global-container.test.ts | 49 ++++++++++++++++++++++++++ src/decorators/inject.ts | 14 ++++++-- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/src/__tests__/global-container.test.ts b/src/__tests__/global-container.test.ts index 35a9c12..9265211 100644 --- a/src/__tests__/global-container.test.ts +++ b/src/__tests__/global-container.test.ts @@ -680,6 +680,55 @@ test("allows explicit array dependencies to be resolved by inject decorator", () expect(bar.foo === fooArray).toBeTruthy(); }); +test("allows inject to provide a default value, which will be used if not registered in other ways", () => { + @injectable() + class Foo {} + + const fooArray = [new Foo()]; + + @injectable() + class Bar { + constructor(@inject("FooArray", {useValue: fooArray}) public foo: Foo[]) {} + } + + globalContainer.register(Bar, {useClass: Bar}); + + const bar = globalContainer.resolve(Bar); + expect(bar.foo === fooArray).toBeTruthy(); +}); + +test("allows inject to provide a default value, if injected afterwards it will be overwritten", () => { + @injectable() + class Bar { + constructor( + @inject("MyString", {useValue: "MyDefaultString"}) public foo: string + ) {} + } + const str = "NewString"; + globalContainer.register("MyString", {useValue: str}); + + globalContainer.register(Bar, {useClass: Bar}); + + const bar = globalContainer.resolve(Bar); + expect(bar.foo === str).toBeTruthy(); +}); + +test("allows inject to have other kinds of provider", () => { + @injectable() + class Bar implements IBar { + public value = ""; + } + + @injectable() + class FooWithInterface { + constructor(@inject("IBar", {useClass: Bar}) public myBar: IBar) {} + } + + const myFoo = globalContainer.resolve(FooWithInterface); + + expect(myFoo.myBar instanceof Bar).toBeTruthy(); +}); + // --- @injectAll --- test("injects all dependencies bound to a given interface", () => { diff --git a/src/decorators/inject.ts b/src/decorators/inject.ts index fd746e1..bd8bcb3 100644 --- a/src/decorators/inject.ts +++ b/src/decorators/inject.ts @@ -1,14 +1,22 @@ -import {defineInjectionTokenMetadata} from "../reflection-helpers"; +import {instance as globalContainer} from "../dependency-container"; +import {Provider} from "../providers"; import InjectionToken from "../providers/injection-token"; +import {defineInjectionTokenMetadata} from "../reflection-helpers"; /** - * Parameter decorator factory that allows for interface information to be stored in the constructor's metadata + * Parameter decorator factory that allows for interface information to be stored in the constructor's metadata. + * + * If a defaultValue is given it will be registered into the global container. * * @return {Function} The parameter decorator */ function inject( - token: InjectionToken + token: InjectionToken, + defaultProvider?: Provider ): (target: any, propertyKey: string | symbol, parameterIndex: number) => any { + if (defaultProvider !== undefined) { + globalContainer.register(token, defaultProvider as any); + } return defineInjectionTokenMetadata(token); } From 26df43c9356042adce006a6a36d587159f88faaf Mon Sep 17 00:00:00 2001 From: Bernat Felip Date: Mon, 26 Apr 2021 17:51:52 +0200 Subject: [PATCH 2/3] Fixed the comment --- src/decorators/inject.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/decorators/inject.ts b/src/decorators/inject.ts index bd8bcb3..06c60ef 100644 --- a/src/decorators/inject.ts +++ b/src/decorators/inject.ts @@ -6,7 +6,7 @@ import {defineInjectionTokenMetadata} from "../reflection-helpers"; /** * Parameter decorator factory that allows for interface information to be stored in the constructor's metadata. * - * If a defaultValue is given it will be registered into the global container. + * If a defaultProvider is given it will be registered into the global container. * * @return {Function} The parameter decorator */ From 0557efe865921775b35b76b9a971dc8d8c91cf6e Mon Sep 17 00:00:00 2001 From: Bernat Felip Date: Sun, 27 Feb 2022 20:36:37 +0100 Subject: [PATCH 3/3] Documentation update --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1fd96d0..0bc32b5 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ constructor injection. - [TSyringe](#tsyringe) - [Installation](#installation) + - [Babel](#babel) - [API](#api) - [Decorators](#decorators) - [injectable()](#injectable) @@ -18,8 +19,8 @@ constructor injection. - [autoInjectable()](#autoinjectable) - [inject()](#inject) - [injectAll()](#injectall) - - [injectWithTransform()](#injectWithTransform) - - [injectAllWithTransform()](#injectAllWithTransform) + - [injectWithTransform()](#injectwithtransform) + - [injectAllWithTransform()](#injectallwithtransform) - [scoped()](#scoped) - [Container](#container) - [Injection Token](#injection-token) @@ -31,8 +32,8 @@ constructor injection. - [Child Containers](#child-containers) - [Clearing Instances](#clearing-instances) - [Circular dependencies](#circular-dependencies) - - [The `delay` helper function](#the-delay-helper-function) - - [Interfaces and circular dependencies](#interfaces-and-circular-dependencies) + - [The `delay` helper function](#the-delay-helper-function) + - [Interfaces and circular dependencies](#interfaces-and-circular-dependencies) - [Disposable instances](#disposable-instances) - [Full examples](#full-examples) - [Example without interfaces](#example-without-interfaces) @@ -193,7 +194,7 @@ need to make the parameters optional, e.g. `database?: Database`. ### inject() Parameter decorator factory that allows for interface and other non-class -information to be stored in the constructor's metadata. +information to be stored in the constructor's metadata. It also accepts a [provider](#providers) as a second parameter, which will be registered within the global container and can act as a fallback value in case no other registrations are done. #### Usage