Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .toDynamicValueWithDeps into BindingToSyntax #1506

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- Added `.toDynamicValueWithDeps` to create a dynamic value with declaratively listed dependencies

### Changed

Expand Down
18 changes: 18 additions & 0 deletions src/interfaces/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,13 @@ namespace interfaces {
toSelf(): BindingInWhenOnSyntax<T>;
toConstantValue(value: T): BindingWhenOnSyntax<T>;
toDynamicValue(func: DynamicValue<T>): BindingInWhenOnSyntax<T>;
toDynamicValueWithDeps<Deps extends readonly ServiceIdentifier[]>(
dependencies: Deps,
func: (
dependencies: ResolvedDeps<Deps>,
context: interfaces.Context
) => T
): interfaces.BindingInWhenOnSyntax<T>;
toConstructor<T2>(constructor: Newable<T2>): BindingWhenOnSyntax<T>;
toFactory<T2, T3 extends unknown[] = unknown[], T4 extends unknown[] = unknown[]>(
factory: FactoryCreator<T2, T3, T4>): BindingWhenOnSyntax<T>;
Expand Down Expand Up @@ -369,6 +376,17 @@ namespace interfaces {
userGeneratedMetadata: MetadataMap;
}

export type ResolvedDeps<Deps extends readonly ServiceIdentifier[]> = {
[P in keyof Deps]: Deps[P] extends string
? unknown
: Deps[P] extends symbol
? unknown
: Deps[P] extends interfaces.Newable<infer R1>
? R1
: Deps[P] extends interfaces.Abstract<infer R2>
? R2
: unknown;
};
}

export { interfaces };
12 changes: 12 additions & 0 deletions src/syntax/binding_to_syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ class BindingToSyntax<T> implements interfaces.BindingToSyntax<T> {
return new BindingInWhenOnSyntax<T>(this._binding);
}

public toDynamicValueWithDeps<Deps extends readonly interfaces.ServiceIdentifier[]>(
deps: Deps,
func: (dependencies: interfaces.ResolvedDeps<Deps>, context: interfaces.Context) => T
): interfaces.BindingInWhenOnSyntax<T> {
return this.toDynamicValue((context) => {
const resolvedDeps = deps.map((identifier) =>
context.container.get(identifier)
) as unknown as interfaces.ResolvedDeps<typeof deps>;
return func(resolvedDeps, context)
})
}

public toConstructor<T2>(constructor: interfaces.Newable<T2>): interfaces.BindingWhenOnSyntax<T> {
this._binding.type = BindingTypeEnum.Constructor;
this._binding.implementationType = constructor as unknown as T;
Expand Down
28 changes: 28 additions & 0 deletions test/container/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1172,4 +1172,32 @@ describe('Container', () => {
expect(() => myContainer.resolve(CompositionRoot)).not.to.throw;
})

it('should be able to resolve all dependencies using toDynamicValueWithDeps', () => {
abstract class AbstractShuriken {}

abstract class AbstractKatana {}

@injectable()
class Shuriken implements AbstractShuriken {}

@injectable()
class Katana implements AbstractKatana {}

class Ninja {
public constructor(public shuriken: AbstractShuriken, public katana: AbstractKatana) {}
}

const container = new Container()
container.bind(AbstractShuriken).to(Shuriken)
container.bind(AbstractKatana).to(Katana)
container.bind(Ninja).toDynamicValueWithDeps(
[AbstractShuriken, AbstractKatana] as const,
([shuriken, katana]) => new Ninja(shuriken, katana)
)

const ninja = container.get(Ninja)
expect(ninja.shuriken).to.be.instanceOf(Shuriken)
expect(ninja.katana).to.be.instanceOf(Katana)
})

});
10 changes: 10 additions & 0 deletions wiki/value_injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ container.bind<Katana>("Katana").toDynamicValue((context: interfaces.Context) =>
// a dynamic value can return a promise that will resolve to the value
container.bind<Katana>("Katana").toDynamicValue((context: interfaces.Context) => { return Promise.resolve(new Katana()); });
```

Binds an abstraction to a dynamic value with required dependencies from the container in a declarative way.
```ts
container.bind(AbstractShuriken).to(Shuriken)
container.bind(AbstractKatana).to(Katana)
container.bind(Ninja).toDynamicValueWithDeps(
[AbstractShuriken, AbstractKatana] as const,
([shuriken, katana]) => new Ninja(shuriken, katana)
)
```