Simple Dependency Resolution for Node Modules
A copy of the licence is within the package source.
This is a Dependency Injection (DI) package for Inversion of Control (IoC) operating at the module level. It currently does automatic parameter injection for all registered modules, plus property injection for visible properties having known names.
Constructor Injection works if your module is a factory, as the create method (or equivalent) is effectivly a module function that can receive injected parameters.
Property Injection is now supported also. Any property at the top level whose name matches that of a something registered and whose value is null will get an injection.
Simply declare properties like
permissionRepository:null and it will just work - provided your dependencies are registered in the correct order.
NoDice is intended for server code and requires parameter names to match the names of registrations. For this reason, minification of parameter names will break the injection as the names will no longer match.
- Parameter injection for all top-level module/object functions.
- Property injection based on exported properties with null values.
- Service locator using
resolve()for other situations.
- Currently, parameter(s) that are to be injected must be the last parameter(s) listed in a function declaration. This is mostly an aesthetics concern.
It's an npm module:
npm install nodice
example subfolder there is an example of a multi-layered codebase with a presentation layer, service, repository and constants. That example includes another README file with fuller details.
container, unlike with C# containers (for example) has no lifetime enumeration/declaration. The registrations are by reference and as long as those references remain (or the container/registration is in scope), so do the registrations.
- Declared without a
containergains global scope.
- Declared with a
It is therefore possible to have local (or even module-level private) containers in addition to a possible global one.
It is strongly recommended that you check the
example subfolder's code and README for clarity; usage is simpler than the syntax might imply.
Reference the Module
container = ; // global
var container = ; // scoped
Register a Dependency
Usually the returned result of a require statement, this is the module/object which will be returned when the registration is resolved.
Due to the nature of Node modules this should either be treated as a singleton or coded as a factory.
Optional Manual Resolve
var thing = container;return thing;
This will return the resolution of the dependency requested. The name is the textual name provided at registration, which may not necessarily be the same as the module's internal name.
If the thing being resolved also has injections, they will be actioned automatically.
Check Registered Modules
var registrations = container;
This returns a simple array of the names that have been registered.
You can run the tests using:
There is an example subfolder which contains a trivial but demonstrative example (based on the examples above) and whose code is both simple and instructive:
npm run example
It has it's own README with more information.