@onr/inversify-binding
v1.0.2
Published
inversify module library
Downloads
23
Readme
inversify-binding
inversify di library easy use
no need to reimport
reflect-metadata
Usage
please check inversify see how to create module
import { inject, injectable } from 'inversify';
import { createContainerModule, createCoreContainer } from '../src/index';
const keyA = 'serviceA';
const keyB = 'serviceB';
interface IServiceA {
getString(): string;
}
interface IServiceB {
getString(): string;
getStringFromA(): string;
}
@injectable()
class serviceA implements IServiceA {
getString() {
return 'serviceA';
}
}
@injectable()
class serviceB implements IServiceB {
@inject(keyA)
private _serviceA!: serviceA;
getString() {
return 'serviceB';
}
getStringFromA() {
return this._serviceA.getString();
}
}
const coreContainer = createCoreContainer()
// nextjs page a using this
const serviceAModule = createContainerModule([keyA, serviceA])
// nextjs page b using this
const serviceBModule = createContainerModule([keyA, serviceA], [keyB, serviceB])
- Why splitting
serviceAModule
andserviceBModule
but both of them importserviceA
?
make this can be easily code splitting in nextjs, then each page can inject the service module and avoid the repeated import.
load module
coreContainer.load(serviceAModule);
coreContainer.load(serviceBModule);
How to use in react component?
please check inversify-react