A simple dependency injection implementation.
This is not intended for production use.
$ npm i @testingrequired/ioc
Define components with the component
decorator.
import { component, register } from "@testingrequired/ioc";
@component
class Component {}
Components can also be defined using the register
function for more control.
import { component, register, instance } from "@testingrequired/ioc";
class RandomEachTime {}
register(
RandomEachTime,
() => {
const component = new RandomEachTime();
component.value = Math.floor(Math.random() * (10000 - 0) + 0);
return component;
},
{ lifetime: instance }
);
The inject
function defines a dependency to a component.
import { component, inject, resolve } from "@testingrequired/ioc";
@component
class Child {}
@component
class Parent {
@inject(Child) child;
}
const parent = resolve(Parent);
parent.child instanceof Child === true;
See the working example
A container's register function can be passed a custom factory even after dependency resolution. This factory can return a mock/spy/stub.
import { makeContainer } from "@testingrequired/ioc";
const container = makeContainer();
@container.component
class Child {}
class Parent {
@container.inject(Child) child;
}
class Sibling {}
container.register(Child, () => new Sibling());
const parent = containter.resolve(Parent);
parent.child instanceof Sibling === true;
Build project in to dist
$ npm run build
Build project when files change.
$ npm run build -- -w
Run tests
npm run test
Run example app.
This will use current build in dist
.
$ npm run example
Register a class as a component.
import { register } from "@testingrequired/ioc";
class Foo {}
register(Foo);
The class, string or symbol used to identify the component.
Function to be called when creating instance of component. This defaults to calling new
on component.
There are two lifetime options: session
(one instance for every resolve) & instance
(new instance on each resolve).
import { register, session, instance } from "@testingrequired/ioc";
class Singleton {}
register(Singleton, null, { lifetime: session });
class NewEveryTime {}
register(NewEveryTime, null, { lifetime: instance });
Register a functional component.
import { component, register } from "@testingrequired/ioc";
@component
class ComponentA {}
@component
class ComponentB {}
export default register.fn(
[ComponentA, ComponentB],
(componentA, componentB) => /* ...implementation */
);
Return instance of component.
import { resolve } from "@testingrequired/ioc";
const foo = resolve(Foo);
Register a class as an injectable component.
import { component } from "@testingrequired/ioc";
@component
class Foo {}
Pass options while registering a component.
import { component } from "@testingrequired/ioc";
@component({})
class Foo {}
There are two lifetime options: session
(one instance for every resolve) & instance
(new instance on each resolve).
import { component, session, instance } from "@testingrequired/ioc";
@component({ lifetime: session })
class Singleton {}
// same as:
@component.session
class Singleton {}
// ---
@component({ lifetime: instance })
class NewEveryTime {}
// same as:
@component.instance
class NewEveryTime {}
Injects initialized component.
import { inject } from "@testingrequired/ioc";
class Bar {
@inject(Foo) foo;
}
Create a container to manage components.
import { makeContainer } from "@testingrequired/ioc";
const container = makeContainer();
@container.component
class Foo {}
class Bar {
@container.inject(Foo) foo;
}