1.0.1 • Published 8 years ago

injectly v1.0.1

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

Injectly

Injectly is a lightweight dependency injector based on Angular 2's dependency injection system.

Similar to angular, Injectly uses providers to store different types of values into the DI container. Injectly supports classes, factory functions and arbitrary values.

Injector API

bindProvider(provider: Provider);
bind(name: string, dependencies: string[], config?: IInjectableConfig);
resolve<T>(name: string): T;
inject(dependencyNames: string[], callback: any;

Values

import {Injector} from 'injectly';

let injector = new Injector();

injector.bindProvider(new Provider('Value', {
    useValue: "Hello World"
}));

injector.bindProvider(new Provider('function', {
    useValue: () => { return 5; }
}));

Factories

import {Provider} from 'injectly';

injector.bindProvider(new Provider('LogFactory', {
    dependencies: ['Value'],
    useFactory: (value) => {
        console.log(`LogFactory: ${value}`); // Hello World
        return value;
    }
}));

Classes

import {Injectable} from 'injectly';

// Using decorators

@injector.bind('A', [])
class A {
    constructor() {
        console.log('A was instantiated');
    }
}

// Create singletons by specifiying useExisting
@injector.bind('B', ['A'], { useExisting: true })
class B {
    constructor(a: A) {
        console.log('Test was instantiated with', A);
    }
}

// Without decorators

class A {
    constructor() {
        console.log('A was instantiated');
    }
}

injector.bindProvider(new Provider('A', {
    useClass: A
}));

class B {
    constructor(a: A) {
        console.log('Test was instantiated with', A);
    }
}

injector.bindProvider(new Provider('B', {
    dependencies: ['A'],
    useExisting: B
}));

Retrieving

// Get dependency from container
let B = injector.resolve<B>('B');

// Inject dependencies into a callback
injector.inject(['A', 'B'], (a, b) => {
    console.log(a); // A { }
    console.log(b); // B { }
});