AdonisJs Fold 🚀
Dependency manager and IoC container for Node.js
Fold is a dependency manager for Node.js used by AdonisJs framework. Below is the list of features.
Features
- Support for binding dependencies with unique namespaces.
- Autoloading multiple directories under a namespace.
- Defining aliases for bindings.
- Automatic resolution of namespaces and transparent dependency injection.
- Support for
fakes
when writing tests. - Support for service providers, to bind dependencies in structured way.
Installation
You can install the package from npm.
npm i --save adonis-fold
Basic Usage
const { ioc } = require('adonis-fold')
class Foo {
}
ioc.bind('App/Foo', function () {
return new Foo()
})
const foo = ioc.use('App/Foo')
ioc.with('App/Foo', (Foo) => {
})
Simple enough! But we do not see the real power of the Ioc container, since we can instantiate the class manually too. Right? NO
Here are the following benefits.
-
The author of the Foo
class can decide how to instantiate the class and return a properly configured instance, instead of leaving it to the consumer.
-
While you are making use of the Ioc container, one binding can be dependent upon others, without much work. For example
class Foo {
constructor (config) {
}
}
ioc.bind('App/Foo', function (app) {
const config = app.use('App/Config')
return new Foo(config)
})
const foo = ioc.use('App/Foo')
This time, we injected App/Config
behind the scenes and the consumer of the Foo
class won't have to worry about passing the config manually.
Moving Forward
Checkout the official documentation at the AdonisJs website for more info.
Tests
Tests are written using japa. Run the following commands to run tests.
npm run test:local
npm run test
npm run test:win
Release History
Checkout CHANGELOG.md file for release history.
Meta
AdonisJs – @adonisframework – virk@adonisjs.com
Checkout LICENSE.txt for license information
Harminder Virk (Aman) - https://github.com/thetutlage