Service Container

Service Container

The framework needs access to knowledge about your custom configuration and implementation of services, which is naturally impossible to provide in advance.
For this reason, an Inversion of Control (or IoC) container is required. Inversion of Control allows the framework to "inject" your application settings into it's core services without needing to know what they are in advance.

TSyringe

After experimenting with several popular IoC containers, we settled on TSyringe which is extremely light-weight, TypeScript from the ground up, and maintained by Microsoft.
TSyringe performs Constructor Injection on the constructors of decorated classes which overrides a Class constructor with typed definitions of the services the class requires.

Resolving Services

For convenience, we wrap the common framework functions such as Browsers, Acts, and Auditions in the Application class (which is auto-injected into most concrete classes), but dependencies can be resolved manually from anywhere in the application.

From within an auto-injected class:

1
import { Command, CommandContract, Browser, Browsers, Page }
2
from "@masquerade/backstage"
3
4
// Auto-injection of this.app occurs in the upstream Backstage Command class.
5
class FooCommand extends Command implements CommandContract {
6
7
public async run() {
8
// Other logic...
9
const browser: Browser = await this.app.browser(Browsers.Chrome)
10
const page: Page = await browser.newPage()
11
}
12
13
}
Copied!

Manually:

1
import { Symbols, ActContract } from "@masquerade/backstage"
2
3
const Acts: ActContract = container.resolve<ActContract>(Symbols.Acts)
Copied!
If you haven't worked with Dependency Injection before, this might feel strange (as opposed to simply importing the class), but it allows us and other developers to provide complex Services and Packages to your application that are automatically configured according to your specific application configuration.
You can read more about TSyringe on their GitHub repo, or StackOverflow.