nodejs-ioc-container
This project will be supported:
- Ioc Container
- Lazy Dependencies loading
Usage
Create a Container
A Container is a single unit of dependency handling. It registers dependencies and resolves them for dependency consumers.
var container = ; container
Register a Dependency
container;
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 v1.0.0 - https://github.com/dexter0201/nodejs-ioc-container/releases/tag/v1.0.0
Consume dependencies
get
Use get
when you need to check if the dependency is registered, because get
returns immediately.
container;
In the above example, if the get
is called before connection is registered, you will get null
.
resolve
If you have a function that needs a connection, use
container;
resolve
also supports the "Array notation":
container;
and, of course, the "dependable notation":
container;
tldr: if 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:
container;