Skip to main content

Examples

DependencyManager usage with wraplets

DependencyManager used implicitly with wraplet

The default Dependency Manager implementation provided by the Wraplet library is called Core.

AbstractDependentWraplet uses this implementation implicitly when you instantiate your wraplets with AbstractDependentWraplet's protected methods.

For example:

DependencyManager used explicitly with wraplet

Dependency maps

Nested dependent wraplets

Recursive dependent wraplets

This one is tricky because TypeScript doesn't really like recursion, but it's doable!

  1. Note that all dependencies are optional. This is required because we don't know how deep the dependency tree is.
  2. Note that the DDM.createInjector in the containerMap function gets a number as the argument. This number references the map's ancestor relative to the current map. It basically allows a map to be recursive. 2 means that we repeat the last 2 levels of the map.
  3. Note that dependency maps have to be additionally statically typed. This is because TypeScript doesn't really like recursion, and needs a bit more guidance to figure out all the types here.

Advanced dependency management

Injecting external dependency

In this example we assume that we already have an initialized dependency, and we want to inject it into our wraplet.

There are other methods for injecting existing dependencies possible:

  • Using a setter on an already instantiated wraplet. This one has a drawback that the dependency in the map would have to be set as optional because the wraplet would be instantiated without it, and only injecter later.
  • Injecting the dependency in the constructor and keeping a reference to it in a custom property. This is fine, but it bypasses the dependency manager and its features.