Protected
parametersProtected
dependenciesProtected
servicesProtected
overidesPrivate
#servicePrivate
#compiledRegisters a class as a service. You can also use singleton and transient.
Throws ArgumentCountError if the given dependencies do not match the required constructor arguments.
Throws CyclicalDependencyError if there is a self-referencing constructor argument.
// Logger depends on some log level constant
container.register(LoggerService, [LogLevel.DEBUG]);
// Database depends on Logger
container.register(Database, [LoggerService]);
Registers a class as a singleton service.
Shortcut for register with Lifetime.SHARED.
Registers a class as a transient service.
Shortcut for register with Lifetime.TRANSIENT.
Overrides a service with another concrete implementation.
Note: Javascript does not support interfaces (i.e. you cannot pass a TS
interface by value). Therefore, you should first register()
a
'base class' as a default implementation, after which you can
override it using override()
.
You may omit the 'dependencies' argument to match the constructor signature of the overridden class.
class IFoo {
someMethod(): void {}
}
// First register the default implementation / 'base class' for IFoo.
container.transient(IFoo, []);
container.singleton(MyService, [IFoo]); // MyService depends on IFoo
container.transient(ConcreteFoo, []); // Register an override service
container.override(IFoo, ConcreteFoo); // ConcreteFoo will be passed to MyService
Retrieves a service from the container, and resolves it's dependencies.
Will throw a ServiceNotFoundError if the service was not registered via register.
const instance = container.get(MyClass);
Private
createGenerated using TypeDoc
Minimal Dependency Injection container.
Example