Start implementation of model relations
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
37
src/di/InjectionAware.ts
Normal file
37
src/di/InjectionAware.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import {Container} from './Container'
|
||||
import {TypedDependencyKey} from './types'
|
||||
|
||||
/**
|
||||
* Base class for Injection-aware classes that automatically
|
||||
* pass along their configured container to instances created
|
||||
* via their `make` method.
|
||||
*/
|
||||
export class InjectionAware {
|
||||
private ci: Container
|
||||
|
||||
constructor() {
|
||||
this.ci = Container.getContainer()
|
||||
}
|
||||
|
||||
/** Set the container for this instance. */
|
||||
public setContainer(ci: Container): this {
|
||||
this.ci = ci
|
||||
return this
|
||||
}
|
||||
|
||||
/** Get the container for this instance. */
|
||||
public getContainer(): Container {
|
||||
return this.ci
|
||||
}
|
||||
|
||||
/** Instantiate a new injectable using the container. */
|
||||
public make<T>(target: TypedDependencyKey<T>, ...parameters: any[]): T {
|
||||
const inst = this.ci.make<T>(target, ...parameters)
|
||||
|
||||
if ( inst instanceof InjectionAware ) {
|
||||
inst.setContainer(this.ci)
|
||||
}
|
||||
|
||||
return inst
|
||||
}
|
||||
}
|
||||
@@ -13,3 +13,4 @@ export * from './ScopedContainer'
|
||||
export * from './types'
|
||||
|
||||
export * from './decorator/injection'
|
||||
export * from './InjectionAware'
|
||||
|
||||
Reference in New Issue
Block a user