LentilDI
Lightweight + Simple ES6 Dependency Injection :)
LentilDI lets you build apps without the pain of having to instantiate, wire up, and manually manage your dependency tree. LentilDI emphasises:
- Ease of module testing
- Reduction of boilerplate dependency juggling
Check out the hello world example for a quick introduction.
Install
$ npm install --save lentildi
Example
With LentilDI, you can go from something like this:
const tuba = ;const horn = ;const percussion = ;const conductor = 'Snoop Dogg';const brassSection = conductor tuba horn fs os;...const orchestra = conductor brassSection percussion;
To something like this:
const lentil = ;lentil;const orchestra = lentil;
Automatic Dependency Wiring
Typically, we might pass in our dependencies (including built-in objects) and bind them to this
in large constructors such as this:
{ thisconductor = conductor; thistuba = tuba; thishorn = horn; thisfs = fs; thisos = os; } { const sheetMusic = thisconductor; thisfs
When we use LentilDI, we get that wiring done for us for free:
static { return conductor: Conductor tuba: Tuba horn: Horn fs os } { const sheetMusic = thisconductor; thisfs
Testing
Testing modules is easy as pie - just create your module as normal!
If you want to override anything in lentilDeps
, just pass an object as the last argument to your constructor.
;
More Examples
Check out some full example apps (with tests!) here.
LentilDep
You can specify different types of dependencies in your lentilDeps
declaration.
Currently, Lentil understands 3 types of dependencies:
LentilDep.Provided
LentilDep.Regular
LentilDep.Lentil
LentilDep.Provided
For cases where you might have an externally instantiated class (e.g. a logger) that you want to be available in any of your modules:
const logger = log4js; const lentil = ;lentil;const myApp = lentil;
Your logger instance will now be available as normal through this.logger
inside a Lentil module:
static { return logger: LentilDep } { thislogger; }
LentilDep.Regular
This is the default type where values are simply passed along to your module.
Unless otherwise specified, this is how Lentil will treat a dependency.
static { return whatever: 'Whatever' }
'Whatever' would now be available through this.whatever
. (This is particularly useful for built in objects such as os
, console
etc.)
For the sake of clarity, note that this is functionally equivalent to the following:
static { return // This is not recommended as Lentil can do this wrapping for us. whatever: LentilDep }
LentilDep.Lentil
For sub-dependencies that you wish Lentil to also construct (i.e. other modules that extend from LentilBase.)
Similar to LentilDep.Regular, you do not need to explicitly wrap modules in this; Lentil will do this for you.
static { return someModule: SomeModule }
This is equivalent to the following:
static { return // This is not recommended as Lentil can do this wrapping for us. someModule: LentilDep }
Constructor Arguments
You can pass in arguments to your modules (useful for one-offs such as config values).
To do so, pass an array of arguments to lentil.setArgs
:
const lentil = ;lentil;...
Inside your module, your arguments are available as normal.
(Remember to call super
.)
{ super...args; console; } ...
Full Documentation
Coming Soon
Contributing
Please do!
Why 'Lentil'?
I like lentils