@produck/kit
v0.1.7
Published
A useful dependency injection module.
Downloads
5
Readme
@produck/kit
A module to create a injection for implemention of DI, IoC. It can easily build injection prototype chain. Each injection represents a job space, a problem scope. Child injection can access dependencis of its prototype injection.
It has been published as a "Dual CommonJS/ES module" package but ESM first. It can work in "node.js" and browsers. It is also very friendly with "tree shaking", using "Rollup".
A Kit, the injection is IMMUTABLE
.
Installation
$ npm install @produck/kit
Usage
Import / require
As esModule,
import * as Kit from '@produck/kit';
As CommonJs,
const Kit = require('@produck/kit');
Prepare / Inject / Spread
The chain is like [Child] --|> [Base] --|> [Global]
.
- Inject
[Child]
toFooProvider()
. - Inject
[Base]
toBarProvider()
.
import * as Kit from '@produck/kit';
function BarProvider(Kit) {
console.log(Kit.Kit); // => Child Kit
console.log(Kit.version); // => @produck/kit version
console.log(Kit.foo); // => 'bar'
console.log(Kit.baz); // => 'qux'
}
function FooProvider({ Kit, version, foo }) {
// Spread
console.log(version); // => @produck/kit version.
console.log(foo); // => 'bar'
// Create a child Kit by `Base Kit`
const child = Kit('Child');
child.baz = 'qux';
// Inject
BarProvider(child);
}
// Prepare
const base = Kit.global('Base');
base.foo = 'bar';
// Inject
FooProvider(base);