retax-di
v1.1.2
Published
Dependency injection for retax
Downloads
37
Maintainers
Readme
Retax-Di
This library allows you to create and mediate an IoC container (based on inversify). It also allows the user to register services into it (usefull if you want to register component into the container at runtime).
It is used by retax-client and retax-server.
It depends on retax-core.
How does it work?
The user can optionnaly register a service using the injector
:
import { InversifyKernelFacade } from 'retax-core';
import { Injector, KernelMediator, KernelFactory } from 'retax-di';
class Incrementer {
public value: number = 0;
public increment(): number {
return ++this.value;
}
}
class Decrementer {
public value: number = 0;
public decrement(): number {
return --this.value;
}
}
// you could create a top-level IoC container if you don't want to do the following yourself
const inversifyKernelFacadeFactory = () => new InversifyKernelFacade();
const kernelFactory = new KernelFactory(inversifyKernelFacadeFactory);
const injector = new Injector();
const kernelMediator = new KernelMediator(kernelFactory, injector);
const incrementerId = injector.registerService(Incrementer);
const kernel = kernelMediator.create(/* optionnal kernel modules (see https://github.com/inversify/InversifyJS/#declaring-kernel-modules*/);
const incrementer = kernel.get(incrementerId);
assert(incrementer.value === 0);
assert(incrementer.increment() === 1);
// Later...
const decrementerId = injector.registerService(Decrementer);
kernelMediator.reload(kernel); // reload will autmatically reload user modules into the kernel
const decrementer = kernel.get(decrementerId);
assert(decrementer.value === 0);
assert(decrementer.increment() === -1);
FAQ
I don't understand how this library is built
Check builder and builder-ts-library
Typescript support
This project is shipped with typescript typings. If you are using typescript@^1.6, you don't have to do anything, it will detect the definition types automatically.
##License MIT License (MIT)