Creates a new DI Container and registers itself.
You may also use the globally available instance, see container.
Protected
parametersProtected
dependenciesProtected
servicesProtected
overidesPrivate
#servicePrivate
#compiledProtected
extensionsProtected
extensionPrivate
#extRegisters 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);
Add an extension bundle to the container.
You may pass configuration overrides via the config
parameter.
See also: BundleInterface.
Optional
config: BundleConfigType<T>Retrieves an extension bundle from the container.
// Basic usecase
import { MyBundle } from '.';
const ext = container.getExtension(MyBundle);
// Or use `import type` to aid tree-shaking
import type { MyBundle } from '.';
const ext = container.getExtension<MyBundle>('MyBundle');
Set a superglobal.
Can be useful for general application config. Only use this if the parameter is 'global', shared across multiple services or bundles.
For bundles specifically, if your parameter only applies to a single
module (in a bundle), you should use the bundle's config
,
see BundleInterface.
Get a superglobal.
Can be useful for general application config. Only use this if the parameter is 'global', shared across multiple services or bundles.
For bundles specifically, if your parameter only applies to a single
module (in a bundle), you should use the bundle's config
,
see BundleInterface.
When strict, can throw a ParameterNotFoundError.
Resolves the container.
Generated using TypeDoc
DI container that supports extension bundles. See addExtension and getExtension.
For more information, refer to BundleInterface.