Container Instance
The Service Container can be instantiated like any other regular class. This allows you to use the container in isolation, without application-wide side effects.
import { Container } from "@aedart/container";
const container = new Container();
However, if you want the use the same Service Container instance across your entire application, then you can obtain a singleton instance, via the static method getInstance()
.
The getInstance()
method will automatically create a new Service Container instance and store a static reference to it, if no previous instance was created. Otherwise, the method will return the existing instance.
const container = Container.getInstance();
Destroy Existing Instance
In situations when you need to destroy the existing singleton instance, call the static setInstance()
method with null
as argument.
Container.setInstance(null); // Existing singleton instance is now lost...