This is a
playground to test code. It runs a full
Node.js environment and already has all of
npm’s 400,000 packages pre-installed, including
nodice with all
npm packages installed. Try it out:
This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.
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.
resolve()for other situations.
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.
containergains global scope.
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 = require('nodice'); // global
var container = require('nodice'); // 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.resolve(name); return thing.canAccessArea(1, constants.AdminArea);
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.getRegistrations();
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.