yaioc: Yet Another IOC-Container for Node.js
Goals
- Small
- Unobtrusive, invisible to managed components
- Easy to use
- Pluggable/embeddable by design
- No meta-data or scripting required
- Inspired by PicoContainer
Installation
yaioc is available via npm: npm install yaioc
Usage
// Write components {} { thisfoo = foo; thisvalue = value;} // Assemble in containervar container = yaioc;container;container;container; // Instantiate componentsvar bar = container; ;;;
Of course, you can use more modern constructs like classes and ESModules too.
Naming Conventions
var FooBar = ...; // this refers to a Class/Constructor function named "FooBar"var fooBar = ...; // this refers to an instance of FooBar { ... } // an instance of FooBar will be passed in
Factories
Components can also be registered as Factories, a function which provides the result of a component lookup:
var container = yaioc;container;container; var foo = container; ;
Adaptors
Adaptors work similar to Factories, but are very low level. This is the most basic Interface, an adaptor will have to resolve all dependencies for itself. This is useful if you have to do additional work, or a conditional lookup:
var container = yaioc;container;container;container; var foo = container; ;
There is also an object-oriented interface for adaptors. This is handy for extracting a more advanced calculation or lookup to a separate class.
{} FooAdaptorprototype { var result; // work return result;}; var container = yaioc;container; var foo = container; ;
Additionally an adaptor will get passed the name of its target component:
var container = yaioc;container;container; var foo = container;
Caching
If you want to have a single instance of a component, use caching:
container; var fooOne = container;var fooTwo = container; ;
This works with factories and constructors:
container; var fooOne = container;var fooTwo = container; ;
Component Scan
A convenient way to register many components is the component scan facility.
Internally this uses the glob module, so many wildcard expressions are allowed.
All matched files will be require
d and register
ed.
var container = yaioc;var MyController = ; container; ;;
Container hierarchies
A container can get access to components registered in wrapped containers, but not vice-versa. If you want to wrap multiple containers, pass in an array of containers
var wrappedContainer = yaioc;var container = yaioc; var foo = {} bar = {};wrappedContainer;container; ;;
Dependency Graph
There is a method to give you an overview of all dependencies and transient dependencies of any given component:
{} {} {} var container = yaioc;container;container;container;container;container; var graph = container;console;
This will print a tree of dependencies:
bar
├┬ foo
│└─ baz
└─ value
Additionally, there will be an error if either a dependency is not found, or a circular reference is detected.
Dependency Schema
In order to get an overall picture about the regitstered components and their dependecies
in the container, you can use the method getDependencySchema
:
{} {} {} var container = yaioc;container;container;container;container;container; var schema = container;console;
That outputs content of a Dot File.
digraph yaiocDeps {
foo -> baz;
bar -> foo;
bar -> value;
barDependent -> bar;
}
You can use any cli or online tool to generate the visual output.