1.0.0-alpha • Published 9 years ago

container-ts v1.0.0-alpha

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

container-ts

Lightweight decorator-based Dependency Injection container for Typescript and ES7.

Build Status

Features

  • Dependency Injection via ES6 Decorators
  • Container lifecycle management

API

Container {
    add(item: any): void;
    add(item: any, name: string): void;
    get(ctor: Function): any;
    get(name: string): any;
    has(ctor: Function): any;
    has(name: string): any;
    init(): void;
    destroy(): void;
}

class ContainerBuilder {
    static create(): Container;
}

// decorators
@Inject(selector: () => any | string)
@Destroy
@PostConstruct

Usage

import {ContainerBuilder, Container, Inject, PostConstruct, Destroy} from './container';

@InjectConstructor('ctorDependency1', 'ctorDependency2')
MainClass {

    @Inject('elementId')  // injection by id
    dependency1 : DependencyClass1;

    @Inject(() => DependencyClass2)  // injection by type
    dependency2 : DependencyClass2;

    constructor(a : CtorDependency1, b : CtorDependency2) {
        // ...
    }

    @PostConstruct // invoked after all injections have been resolved
    init() {
        console.log(dependency1.sayHello());
    }

    @Destroy
    destroy() {
        console.log('destroy');
    }
}

let container = ContainerBuilder.create();

container.add(new MainClass());
container.add(new DependencyClass1(), 'elementId');
container.add(new DependencyClass2());

container.add(new CtorDependency1(), 'ctorDependency1');
container.add(new CtorDependency2(), 'ctorDependency2');

container.get('elementId'); // returns the instance of DependencyClass1 added as 'elementId'

container.get(DependencyClass2); // returns the DependencyClass2 instance

container.init();  // output: "Hello!

container.destroy(); // output: "destroy"
1.0.0-alpha

9 years ago

0.7.14

10 years ago

0.7.13

10 years ago

0.7.12

10 years ago

0.7.11

10 years ago

0.7.10

10 years ago

0.7.9

10 years ago

0.7.8

10 years ago

0.7.6

10 years ago

0.7.4

10 years ago

0.7.3

10 years ago

0.7.2

10 years ago

0.7.1

10 years ago

0.7.0

10 years ago

0.6.4

10 years ago

0.6.2

10 years ago

0.6.1

10 years ago

0.6.0

10 years ago

0.5.12

10 years ago

0.5.11

10 years ago

0.5.10

10 years ago

0.5.9

10 years ago

0.5.8

10 years ago

0.5.7

10 years ago

0.5.6

10 years ago

0.5.5

10 years ago

0.5.2

10 years ago

0.5.1

10 years ago

0.5.0

10 years ago

0.4.1

10 years ago

0.4.0

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago