1.1.0 • Published 7 years ago

bubble-di v1.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

bubble-di

A simple, Dependecy Injection Container for Javascript and Typescript. Typings included.

build status

Installation

npm install --save bubble-di

How does it work?

Simple example
var {DiContainer} = require("bubble-di");
// import { DiContainer } from "bubble-di";

DiContainer.setContainer(new DiContainer());

DiContainer.getContainer().registerInstance("foo", () => {console.log("foo");});
const fooFunc = DiContainer.getContainer().resolve("foo");
fooFunc(); // prints 'foo'

class Bar { sayBar() {console.log("bar");}}

DiContainer.getContainer().registerInstance("bar", new Bar());
const bar = DiContainer.getContainer().resolve("bar");
bar.sayBar(); // prints 'bar'
Transitive dependencies
var {DiContainer} = require("bubble-di");
// import { DiContainer } from "bubble-di";

DiContainer.setContainer(new DiContainer());

class Bar { sayBar(){ console.log("bar"); } }
class Baz { sayBaz(){ console.log("baz"); } }
class Foo { 
    constructor (bar, baz)
    {
        bar.sayBar();
        baz.sayBaz();
        // ...
    }
};

DiContainer.getContainer().registerInstance("bar", new Bar());
DiContainer.getContainer().registerInstance("baz", new Baz());
DiContainer.getContainer().register("foo", {
    dependencies: ["bar", "baz"],
    factoryMethod: (bar, baz) => new Foo(bar, baz) },
);
const foo = DiContainer.getContainer().resolve("foo"); // will print "bar" and "baz".
Deriving the DiContainer to postprocess dependencies after being resolved
var {DiContainer} = require("bubble-di");
// import { DiContainer } from "bubble-di";

class MyDiContainer extends DiContainer {
    onResolved(id, resolvedInstance) {
        console.log(`id being resolved: '${id}'`);
        // do stuff with resolvedInstance
    }
}

DiContainer.setContainer(new MyDiContainer ());
DiContainer.getContainer().registerInstance("foo", {});
DiContainer.getContainer().resolve("foo"); // will print "id being resolved: 'foo'"

License

MIT

1.1.0

7 years ago

1.0.0

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago