FoldLoader 🚀
Dependency manager, autoload and IoC container for Node.js
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 foldloader
Setup
using require
Require in main run
package.json
Config in file
root
: namespace Root of appdirectories
: defind folder using
const closure = ioc
preLoadFiles
: files load after run config autoloadautoloads
: defind namespace of folder
Basic Usage
- When
require('flodloader')
in main file, it will register two function globle isuse
andmake
use
function
- file in node_modules
- or file in namespace defind in
autoloads
with structurenamespace + file || namespace + folder + file
make
function - require file, constructor and inject to class
static { return 'App/Bar' } { thisbar = bar } const fooInstance = Ioc
Autoload
Namespace default const ioc resolver registrar =
ioc
Using - method
bind
bind file with file or object
ioc
ioc
- method
singleton
file with file or object asbind
but isdesign pattern singleton
- method
alias
create sholt name
ioc
-
method
use
andmake
-
method
fake
andsingletonFake
reqlace namespace have exits
Ioc
- method
restore
namespace register
Ioc.restore('Adonis/Src/Lucid')Ioc.restore('Adonis/Src/Lucid', 'Adonis/Src/Config')Ioc.restore() // restore all
resolver
Using - method
forDir
get directories was config inpackage.json
const closure = ioc
- method
translate
Translate binding using resolver translate
const closure = ioc
- method
resolve
Resolves the binding from the IoC container. This method is a combination oftranslate
andIoc.make
function.
// class App/User const handler = resolver
- method
resolveFunc
Resolves a function by translating the binding and then validating the existence of the method on the binding object. Also if thebinding
param is a function, it will be recognized and returned.
// with `find` as method of App/User const handlerInstance = resolver
registrar
Using - register provider class extendes to
- all method
register
will call after all methodboot
const ServiceProvider = { // this app as ioc thisapp } { }
- Have register Provider
// add providerconst registrar = registrarreiretrar
Base
with structure folder
App
| index.js
| Model
| User.js
| Helpers
| index.js
start.js
index.js
package.json
- we have require
Model/User.js
anywhere
const foo = ioc
bind
Using const ioc = ioc const foo = ioc// return Foo class instance
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
{ // } ioc const foo = ioc
Tests
Tests are written using japa. Run the following commands to run tests.
npm run test:local # report coverage npm run test # on windows npm run test:win
Extends
adonis-fold – @adonis-fold – virk@adonisjs.com