0.0.4 • Published 6 years ago

kinject v0.0.4

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

Kinject

Minimalistic dependency injector, the K stands for KISS.

Build Status Coverage Status

Description

A simple dependency injector inspired by Angular ones, lets you create service locators where providers are registered and from with classes are instantiated with its dependencies injected on the constructor.

Setup

npm install --save kinject

If your target browsers doesn't support Proxy you can lazy load the polyfill like this:

if(!window.proxy){
    var script = document.createElement('script');
    script.src = 'proxy.min.js';
    document.body.appendChild(script);
}

Example

const container1 = new Kinject();
const container2 = new Kinject();
container2.linkTo(container1);

class Foo {}
container1.provide(Foo, 'Foo', /* transient */ true);

class Bar {
    constructor(foo) {
        this.foo = foo;
    }
}
Bar = container2.register(Bar, {
    inject: ['Foo'],
    provide: 'Bar',
});

class Baz {
    constructor(foo, bar) {
        this.foo = foo;
        this.bar = bar;
    }
}
Baz = container2.inject(Baz, ['Foo', 'Bar']);
baz1 = new Baz();
baz2 = new Baz();
console.log(baz1.bar === baz2.bar); // true
console.log(baz1.foo === baz2.foo); //false

API

Create a container

const appContainer = new Kinject();

Register a provider

For registering a provider use the method container.provide which recieves as arguments:

  • ctor: constructor or instance to provide.
  • token: injection token for that provider.
  • transient: default false, if set to true every time a provider registered is injected a new instance will be created. In this case ctor must be instantiable. Default behaviour is to create a singleton the first time the dependency is injected.
class Foo{ }
appContainer.provide(Foo, 'Foo');
appContainer.provide(Foo, 'TransientFoo' , true);

Inject dependencies

When injecting dependencies to a class use the method container.inject passing:

  • ctor: class to which dependencies will be injected in the constructor.
  • dependencies: array of strings representing the providers.

The function will return a proxy that will inject all provided dependencies everytime the class is instantiated. Dependencies will be recieved in the same order that were specified in the dependencies array and starting at the first position of the constructor.

class Bar{
    constructor(foo, someParam){
        this._foo = foo;
    }
}
Bar = appContainer.inject(Bar, ['Foo']);

All in one

If a class acts as a provider and also recieves injected dependencies, you can first use the inject method and then register it as an injectable with provide or use the utility method container.register which takes as arguments:

  • ctor: class to which dependencies will be injected and that will act as a provider
  • ConfigObejct.

    • provide: token for the provider.
    • transient: boolean, default false.
    • inject: array of dependencies tokens to get injected.

This method will return a proxy or the constructor given if no inject was supplied.

 class Baz{
     constructor(foo){
         this._foo = foo;
     }
 }
 Baz = appContainer.register(Baz, {
     provide: 'Baz',
     inject: 'Foo'
 });

Link to a parent container

You can specify a parent of a container, when looking for a provider in the child container, if it is not found, it will be provided from the parent if present.

const appContainer = new Kinject();
appContainer.provide(UserService, 'UserService');
const submoduleContainer = new Kinject();
submoduleContainer.linkTo(appContainer);
UserProfile = submoduleContainer.inject(UserProfile, ['UserService']);