static-inject
TypeScript icon, indicating that this package has built-in type declarations

3.0.6 • Public • Published

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();

Dependencies (0)

    Dev Dependencies (3)

    Package Sidebar

    Install

    npm i static-inject

    Weekly Downloads

    2

    Version

    3.0.6

    License

    ISC

    Unpacked Size

    70 kB

    Total Files

    15

    Last publish

    Collaborators

    • berhalak