This module was built as an in-place replacement for dependable in order to
- Introduce "late resolving" (I preferred the word 'lazy' over 'late', hence the module name). In other words, once a piece of software asks for a dependency to be resolved, lazy-dependable will not complain if the designated dependency is still not registered.
- Introduce more elegant ways for defining dependencies when asking for resolving. The only available mechanism in dependable is through names of the parameters in the dependency handler. This mechanism is severely sub-optimal, because it asks for stringifying the provided handler in order to parse the very parameter names. Moreover, this mechanism limits the handler programmers. tldr: you cannot reuse a single handler for different dependencies, you need to write the same function again, but with different names of input parameters.
Create a Container
A Container is a single unit of dependency handling. It registers dependencies and resolves them for dependency consumers.
var Container = Container; var container = ;container
Register a dependency
connection is either an object or a function that will return a connection object.
You cannot register functions that will depend on other dependencies - this you can do with dependable
get when you need to check if the dependency is registered, because
get returns immediately.
In the above example, if the
get is called before connection is registered, you will get
If you have a function that needs a connection, you
resolve also supports the "Array notation":
and, of course, the "dependable notation":
resolve gets a function as a first parameter, it will stringify/parse it to read the names of dependables you need.
Chain dependency providers
You can register functions that will depend on other dependencies and chain dependency providers. For example, if you cannot provide a
database without obtaining a
configuration first, you register your
database provider in the following manner: