DI Container for Google Closure with automatic registration and resolving based on types
Dependency Injection Container for Google Closure Library
This is standalone Node.js module. For Gulp, use gulp-closure-dicontainer.
var diContainer = require'closure-dicontainer';var src = diContainerfsreadFileSyncdepsPathencoding: 'utf8'baseJsDir: 'bower_components/closure-library/closure/goog'name: 'app.DiContainer'resolve: 'App';fswriteFileSync'build/dicontainer.js' src;
Generated DI container name.
Array of types to be resolved.
How to use DI container in your app.
/**@fileoverview App main method.*/googprovide'app.main';googrequire'app.DiContainer';var container = ;containerconfigure// Inject run-time value when App is going to be instantiated.resolve: App"with":// This is parameter name.element: documentbody// Resolve something with something else.resolve: estestorageStorageas: estestorageLocal// Custom factory when new keyword is not enough.resolve: foouiLightboxreturn lightboxsetSomething;// Resolve interface by something real.resolve: fooIFooreturn ;;var app = containerresolveApp;appstart;;googexportSymbol'app.main' appmain;
There is a pattern: Resolve A as B with C by D.
A is type to be resolved. B is optional type to be returned. C is optional object for run-time configuration, where key is argument name and value is runtime value. D is optional factory method.
Enjoy in action in Este.js.
After DI container file creation, deps.js must be updated, which is task for dev stack. Everything will be available in Este.js soon.