@kingga/kc-container
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

Kings Collection IoC Container

Build Status

A simple IoC container for the kings collection. Inspired by the Laravel container!

Installation

Yarn

yarn add @kingga/kc-container

NPM

npm i @kingga/kc-container

Usage

import Container from 'kc-container';

const container = new Container;

class Foo {
    toString(): string {
        return 'foo';
    }
}

class Faz {
    public readonly foo: Foo;

    public constructor(foo: Foo) {
        this.foo = foo;
    }
}

class State {
    protected hiddenState: object = {};

    public set state(state: object) {
        this.hiddenState = state;
    }

    public get state(): object {
        return this.hiddenState;
    }
}

// Simple bindings.
container.bind(Foo, () => new Foo);
container.bind(Faz.name, (app: Container) => new Faz(app.make<Foo>(Foo)));

// Singletons.
container.singleton(State, () => new State);

// Getting something from the container.
const stateA = container.make<State>(State);
stateA.state = { foo: 'bar' };

const stateB = container.make<State>(State);
it('should be the same since it is a singleton', () => strictEqual(stateA, stateB));

Package Sidebar

Install

npm i @kingga/kc-container

Weekly Downloads

0

Version

1.0.3

License

MIT

Unpacked Size

42.9 kB

Total Files

46

Last publish

Collaborators

  • kingga