mikro-di
Simple ES6 DI container for node.js, that honours convention over configuration.
It works by discovering given directories and generating a simple DI container instance, which is properly type-hinted and allows auto-completion in editors like WebStorm or VS Code.
It also allows to create service definition with scalar constructor parameters
(e.g. HttpClient('https://api.example.io')
) and has built-in circular dependencies
detection.
Inspired by https://github.com/nette/di
Installation
$ yarn add mikro-di
or
$ npm install mikro-di
Usage
DI container should be the first thing you enable in your application, so the right place
for it is in the app.js
file.
app.js
;
And then you can require it and use it:
const di = di; const ret = diYourFunkyService;console;
Registering dependencies
You can declare you service dependencies like this:
/** * @param * @param */ { thisdep1 = dep1; thisdep2 = dep2; } // and export itmoduleexports = YourFunkyService;
Or if you like to you simple objects, you can do this:
const YourFunkyService = $inject: 'YourFunkyDependency1' 'YourFunkyDependency2' /** * @param * @param */ { thisdep1 = dep1; thisdep2 = dep2; } ; // and export itmoduleexports = YourFunkyService;
When you register you service as ES6 class, you do not need the $inject
property,
but the jsdoc with proper type-hinting is required.
If you use plain object export, you can omit the type-hinting of constructor.
Usage in sails.js controllers
const di = di; moduleexports = /** @var */ funkyService: diYourFunkyService { const data = thisfunkyService; res; } ;
The @var
annotation is optional.
Scalar constructor parameters
When you need to pass a scalar constructor parameter to your service, you can
do so be providing a service definition via options
:
/** * @param */ { thisnamespace = namespace; } moduleexports = YourFunkyDependency2;
const di = ; console; // YourFunkyDependency2console; // 'funky-namespace'
Configuration
You can pass your configuration as a second parameter to mikro-di
:
;
How does it work
mikro-di
will discover all paths that you provide as its first parameter and generate
DI context, that you can require and use in your application. It works by loading the contents
of source files via fs
, so the file itself is never loaded (required) before you try to access
it from the di
context.
The context file is generated in your baseDir
and is named as .context.js
. This file should
be ignored via .gitignore
, as it is generated code. You can adjust the path where it is stored
via contextDir
option and the name via contextName
option.
The context file will look like this:
moduleexports = _map: {} /** * @returns */ { if !this_mapYourFunkyService const YourFunkyService = ; this_mapYourFunkyService = thisYourFunkyDependency1 thisYourFunkyDependency2; return this_mapYourFunkyService; } /** * @returns */ { if !this_mapYourFunkyDependency1 this_mapYourFunkyDependency1 = ; return this_mapYourFunkyDependency1; } /** * @returns */ { if !this_mapYourFunkyDependency2 this_mapYourFunkyDependency2 = ; return this_mapYourFunkyDependency2; } ;
It basically creates object with ES6 getters that will require you services and its dependencies. This way everything is loaded via lazy loading technique.
Circular dependencies detection
Before building the container, simple DFS algorithm is used to look up cyclic dependencies and when found, it raises an error.