clever-injector
v1.0.3
Published
Lightning-fast flexible non-blocking Dependency Injection.
Downloads
32
Maintainers
Readme
Cleverstack Injector Module
NodeJS Dependency Injection for the CleverStack ecosystem, designed to work with or without CleverStack.
Important
If you are using the CleverStack framework you do not need to install or setup the injector, CleverStack comes out of the box with the injector ready to use.
Features
- Non-blocking and fully async
- Load's resources based upon name for you and injects dependencies
- Will never load the same resource more than once, it uses the one singular instance for all dependencies
Install
npm i clever-injector
Setup injector instance
var CleverInjector = require( 'clever-injector' );
// You can add directories so that the injector can try to load the resources by name,
// this example presumes you have a folder called src with a file named Example.js
var injector = CleverInjector( __dirname + '/src', __dirname + '/config );
// You can add instances to the injector like this
injector.instance( 'config', config );
injector.instance( 'models', models );
injector.instance( 'db', db );
// You can get instances like this
var config = injector.getInstance( 'config' );
// You can inject functions and name them like this
injector.inject(
function( config, models ) {
return {}; // Return whatever resource you are defining
},
function( Resource ) {
injector.instance( 'Resource', Resource );
}
);
Writing files
// src/Example.js
module.exports = function( db ) {
return {};
};
Use injector
injector.inject( function( Example, models, config ) {
// this function will be called asynchronously after all required modules are initialized and/or loaded.
});
For more details take a look at provided testsuite. There are all possible use cases.