boxed-injector
Dependency Injection Tools
Installation
$ npm install --save boxed-injector
Overview
This is a set of lightweight Dependency Injection components that can be used in both client and server javascript code.
There are 2 components.
- Injector (Dependency Injection Container)
- Locator (Service Locator, which exposes the DI Container)
For client side development with React, there are additional helper utilities in boxed-injector-react
Installation
$ npm install --save boxed-injector
Usage
'use strict'; const parts = ; static { return 'Parts' ; } { thisparts = parts; } static { return 'EngineFactory' ; } { thisengineFactory = engineFactory; } // Dependency Injection Containerconst Injector = Injector;let injector = ; // Register instancesinjector; // Register Factoriesinjector ; const carFactory = injector; console; // Service Locator - warning - this is a singleton!const Locator = Locator;Locator;injector = Locator; const sameCarFactory = injector; console;console; // injector.get('whatever');
Create
Resolves a factory's dependencies and then instantiates an instance with the given args. Will call the constructor (new) on a registered instance that is a function.
{} { thisengine = engine; thiscolor = color; }Carinject = 'Engine';injector;injector;const car = injector;// { engine, engine, color: 'blue' }
Middleware
Middleware functions are executed every time a service is accessed from the container (or on a factory, the first time it's accessed). Global middlewares as well as service / factory specific middlewares are supported and are executed in the order of registry (FIFO). Note that registered instances are singletons and mutations will affect all consumers.
Middlewares are synchronous, and is passed an object as follows:
name: 'ExampleService' depends: 'ThingItsDependentOn' 'OtherThing' instance: thing: {} other: {} // fully instantiated instance, factory: ExampleService // factory
Usage:
const Injector = ;const injector = ; // will console log before getting any instance from the containerinjector;// will console log 'baz' before getting baz from the container - will always run after global aboveinjector;// will console log for any instance, but will run after baz and above global is logged injector; injector; // will console log AFTER getting any instance from the containerinjector;// will console log 'baz' AFTER getting baz from the container - will always run after global aboveinjector; injector; // -> before global// -> baz// -> before global again// instance returned// -> baz// -> after global
Advanced Usage
// call the function directly instead of calling new on it.injector; // declare dependencies during registration rather than on the function.injector; // dependency could be singular.injector; // dependency could be singular.injector; injector;// etc... // graph returns a injector;// ['d', 'c', 'b', 'a'] // true or false if the injector has a service/factory registered.injector; // like injector.register, but actually replaces the instance (no guard)injector;
Contributing
We look forward to seeing your contributions!
License
MIT © Ben Lugavere