Skip to content

Latest commit

 

History

History
30 lines (22 loc) · 1.19 KB

hierarchical_di.md

File metadata and controls

30 lines (22 loc) · 1.19 KB

Support for hierarchical DI systems

Some applications use a hierarchical dependency injection (DI) system. For example, Angular 2.0 applications use its own hierarchical DI system.

In a hierarchical DI system, a container can have a parent container and multiple containers can be used in one application. The containers form a hierarchical structure.

When a container at the bottom of the hierarchical structure requests a dependency, the container tries to satisfy that dependency with it's own bindings. If the container lacks bindings, it passes the request up to its parent container. If that container can't satisfy the request, it passes it along to its parent container. The requests keep bubbling up until we find an container that can handle the request or run out of container ancestors.

let weaponIdentifier = "Weapon";

@injectable()
class Katana {}
 
let parentContainer = new Container();
parentContainer.bind(weaponIdentifier).to(Katana);
 
let childContainer = new Container();
childContainer.parent = parentContainer;

expect(childContainer.get(weaponIdentifier)).to.be.instanceOf(Katana); // true