SDIC: Simple Dependency Injection Container
Dependency injection container with auto injection of module dependencies (by parameter names).
Prerequisites
Node.js version >= 6.0
Install
npm install sdic
Container initialization
const container = ;
or
;const container = sdic;
Container API
Loads single module file or all module files in given path.
- path - path to module file or folder with modules
- opts - options
- alias - module alias (for a single file module) or a basedir alias (for a folder of modules)
- cache - keep instances of modules is cache? (default: true)
- tags[] - list of tags for loaded module(s)
- recursive - recursive loading of folder (default: true)
- filter - loads only modules by regexp filter
- ignore[] - list of regexp to ignore unwanted modules
- reverseName - create a module name "up to down" (default: false)
- prefix - prefix module name
- postfix - postfix module name
- deduplicate - remove multiple same string occurences (default: false)
- uppercaseFirst - create a module name with uppercased first letter (default: false - module name starts with lowercased letter)
- isConstructor - loaded module is a constructor, load and return it as a constructor, do return an instances (default: false - modules as singletons by default)
Registers a single module.
- name - module name
- fn - module body (function, JSON, primitive, ...)
- options - see above
Checks whether container contains given module by name.
Returns registered module from container.
- name - registered module name
- overrides - collection of overridden module dependencies
Returns all registered modules
Returns all registered modules with given tag.
- tag - tag name
Overrides registered module with new instance. Parameters: see register method
Unregisters module from container.
- name - module name to unregister
Removes all modules from container.
Tutorial
Module definition - a module with no dependencies
module { // module instance return {} }
or using ES6 export default syntax:
{ // module instance return {} }
Module definition - a module with some dependencies
// module depends on myDb and myNextServicemodule { // module instance return { return myNextService; } }
or using ES6 export default syntax:
// module depends on myDb and myNextService { // module instance return { return myNextService; } }
Multiple modules definition using ES6 named exports
// module without dependenciesconst firstFunctionalService = { return passed: true }; // module dependds on fooService { return fooServicemethod }; // module dependds on fooService { thisfooService = fooService; } { return thisfooServicemethod; }
ES6 note: container returns instances of modules by default (aka singletons). If you want to register a class (constructor function), you have to use option: isConstructor: true
{}container;container; // --> returns an instance of FooBar (default behaviour)
{}container;container; // --> returns class FooBar
// all modules will be loaded as constructorscontainer;
Manual module registration
container;container; // returns {foo: 'bar'} container;container; // returns 123 container; container;container; // returns an instance of module with injected dependencies
Flat structure loading
Consider this project structure:
+ config.json
|
+ services/
+ - users.js
+ - user-roles.js
|
+ repositories/
+ - roles.js
+ - users.js
|
+ lib/
+ - validator.js
Let's load all files into SDIC.
container; // loads single file: config.jsoncontainer; // loads all files in "services" folder (basedir == "services")container; // loads all files in "repositories" folder (basedir == "repositories")container; // loads all files in "lib" folder (basedir == "lib")
By default, all loaded modules will be named "down to up": camelCased filename + camelCased basedir. So the module names will be:
- config
- userRolesServices
- usersServices
- rolesRepositories
- usersRepositories
- validatorLib
The "plurals" sounds strange. We can rename the basedirs to "singular" or tweak the loader a little bit:
container; // this is OK, keep itcontainer; // alias basedir as "service"container; // alias basedir as "repository"container; // ignore basedir name, there's no need to have explicit "Lib" postfix
Now the module names will be:
- config
- usersService
- userRolesService
- rolesRepository
- usersRepository
- validator
Nice :-)
Nested structure loading
Now consider more nested project structure:
+ services/
+ - users/
+ -- roles.js
+ -- users.js
|
+ repositories/
+ - users/
+ -- roles.js
+ -- index.js
We'll skip "config" file and "lib" folder (see above). Let's load all files into SDIC.
container; // loads all files in "services" folder (basedir == "services")container; // loads all files in "repositories" folder (basedir == "repositories")
By default, all loaded modules will be named "camelCased, down to up": filename + subfolders + basedir. So the module names will be:
- usersUsersServices
- rolesUsersServices
- indexUsersRepositories
- rolesUsersRepositories
Sounds really strange. Let's set up the loading better. The "services" folder first. It'd be cool to start the name with the subfolder (if any), then the filename and the basedir at the end.
container;
Now the modules will be:
- usersService
- usersRolesService
Better, but still sounds strange because of "plurals". To fix this we need to split the loading into two steps:
container;container; // append the ignored file
Finally the modules will be:
- userService
- userRolesService
We can load the "repositories" folder the same way.
ES6 note: when loading named exports into the container, then:
- exported name will be taken instead of a filename (with lowercased first letter)
- filename will be taken for default (not-named) export
Minification
SDIC supports code minification. Because module dependencies are defined using parameter names, the code minification process would damage them (to a
, b
, c
, ... etc.) and SDIC would not be able to load them properly. To prevent this situation all you need to do, is to define the list of a module dependencies in a property dependencies
:
ES6 modules dependencies
const service = (a, b) => { // minified param names
return {
method: () => a.method() + b.method()
};
};
service.dependencies = ['fooService', 'barService']; // <--- definition of dependencies
export default service;
CommonJs modules dependencies
const service = (a, b) => { // minifies param names
return {
method: () => a.method() + b.method()
};
};
service.dependencies = ['fooService', 'barService']; // <--- definition of dependencies
module.exports = service;
Class dependencies
export class ClassService {
constructor(a, b) { // minified param names
this.fooService = a;
this.barService = b;
}
method() {
return this.fooService.method() + this.barService.method();
}
}
ClassService.dependencies = ['fooService', 'barService']; // <--- definition of dependencies
TODO
- load both file and folder with the same name, eg:+- users/ <- load this+- -- foo.js+- users.js <- as well as this
- docs, docs, docs
Based on the idea of: https://www.npmjs.com/package/adctd