DI container
Dependency injection library for js with zero dependencies
Basic usage
import DiContainer from "di-container-js";
// OR
const DiContainer = require("di-container-js").DiContainer;
let diContainer = new DiContainer();
const componentRef1 = Symbol(),
componentRef2 = "component2";
class Component2 {
// define `static dependencies()` method in your class to specify what dependencies are needed
// otherwise required dependecies will be resolved from constructor arguments
static dependencies() {
return [componentRef1];
}
constructor(component1) {
this.component1 = component1;
}
}
class Component1 {
constructor() {
}
}
diContainer.registerClass(componentRef1, Component1);
diContainer.registerClass(componentRef2, Component2);
(async () => {
// .get method returns promise resolved to dependency instance
let component2Instance = await diContainer.get(componentRef2);
// now component2Instance has component1 assigned to it
})();
You can also inject diContainer itself to any component:
constructor(diContainer, ...) {
this.diContainer = diContainer;
...
}
Providing configuration
...
class TestClass {
constructor() {
}
postConstruct(config) {
// gets called by DI container after instance created
// can return promise, in this case DI container will
// wait until promise resolved
return Promise.resolved();
}
}
let diContainer = new DiContainer();
// pass configuration as third parameter to .registerClass
diContainer.registerClass("testDependency", TestClass, {configKey: "configValue"});
// OR
// provide config using .configure method
diContainer.configure("testDependency", {configKey: "configValue"});
(async () => {
let testDependency = await diContainer.get("testDependency");
// do something
})();
Usage with React
To use dependency injection with React class components
you should have a custom ComponentProvider.
Unlike default one, it should construct a react component instead of an instance of a class.
There is a useful example
of inject()
decorator function which uses the custom component provider ReactProvider
and InjectionHoc
high-order component to add dependency injection functionality to React class components. Usage:
import {inject} from '../../di/inject';
class MyComponent extends React.Component {
static dependencies() {
return [/*...*/];
}
}
export default inject(MyComponent);
API Reference
DiContainer
- register(componentRef, componentProvider)
-
Register a dependency using the ComponentProvider
- registerClass(componentRef, classRef, [config])
-
Register a dependency using a class reference
- provide(componentRef, instance)
-
Provide a component instance
- configure(componentRef, config, mergeConfig)
-
Sets a configuration object which will be passed to 'postConstruct' method of a component
-
get(componentRef) ⇒
Promise.<any>
-
Retrieve a component instance
-
constructExternal(classRef, config) ⇒
Promise.<(*|undefined)>
-
Construct an instance of a class without registration in the DI container
-
constructExternalUsingProvider(provider) ⇒
Promise.<(*|un defined)>
-
Construct a component without registration in the DI container
-
createFactory(classRef) ⇒
Promise.<ComponentFactory>
-
Create a factory for a class. A factory allows to create multiple instances of a class and inject dependencies to them. A factory itself is not registered in DI container
-
isInitialized(componentRef) ⇒
boolean
-
isProvided(componentRef) ⇒
boolean
register(componentRef, componentProvider)
Register a dependency using the ComponentProvider
Param | Type | Description |
---|---|---|
componentRef |
symbol | string
|
dependency identifier |
componentProvider | ComponentProvider |
provider which is used to construct a dependency instance |
registerClass(componentRef, classRef, [config])
Register a dependency using a class reference
Param | Type | Description |
---|---|---|
componentRef |
symbol | string
|
dependency identifier |
classRef | function |
reference to a class |
[config] | object |
configuration object which is passed to the postConstruct method of a class after |
it's creation |
provide(componentRef, instance)
Provide a component instance
Param | Type |
---|---|
componentRef |
symbol | string
|
instance | any |
configure(componentRef, config, mergeConfig)
Sets a configuration object which will be passed to 'postConstruct' method of a component
Param | Type | Default | Description |
---|---|---|---|
componentRef |
symbol | string
|
||
config | object |
||
mergeConfig | boolean |
true |
whenever merge new config to old one |
Promise.<any>
get(componentRef) ⇒ Retrieve a component instance
Param | Type |
---|---|
componentRef |
symbol | string
|
Promise.<(*|undefined)>
constructExternal(classRef, config) ⇒ Construct an instance of a class without registration in the DI container
Param | Type |
---|---|
classRef | function |
config | object |
Promise.<(*|undefined)>
constructExternalUsingProvider(provider) ⇒ Construct a component without registration in the DI container
Param | Type |
---|---|
provider | ComponentProvider |
Promise.<ComponentFactory>
createFactory(classRef) ⇒ A factory itself is not registered in DI container class and inject dependencies to them.
Param | Type |
---|---|
classRef | function |
boolean
isInitialized(componentRef) ⇒ Param | Type |
---|---|
componentRef |
symbol | string
|
boolean
isProvided(componentRef) ⇒ Param | Type |
---|---|
componentRef |
symbol | string
|