static-inject
v3.0.6
Published
Dependency injection for nodejs and browser
Downloads
25
Maintainers
Readme
static-inject
Simple dependency injector container.
instalation
npm install static-inject
yarn add static-inject
Sample
Also see src/test.ts
import { Container } from 'static-inject';
// define interface as classes (in order to have a compiled names)
class Port {
lower(text : string) : string { throw 'Not implemented' }
}
@adapter(Port)
class Adapter implements Port {
lower(text: string): string {
return text.toLowerCase();
}
}
class Library {
@port(Port)
private port! : Port;
test(){
if (this.port.lower("UPPER") != "upper"){
throw new Error("Test failed");
}
console.log("Test passed");
}
}
Container.register(Adapter);
// or
Container.registerAs(Adapter, Port.name);
// or
Container.registerAs(Adapter, "Port");
const lib = new Library();
lib.test();