0.0.3 • Published 6 years ago

sapphire-ioc v0.0.3

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

Sapphire IOC

This is a simple IOC container implementation on NodeJS. You can register services on it and retrieve it later.

Register with bind

You can register a service using the bind method and passing a callback on how the service should build when is accessed later. The IOC container will always create a new instance when the ioc.make() is executed.

// SimpleClass.js
class SimpleClass {
    constructor(parameter) {
        this._parameter = parameter
    }

    getParameter() {
        return this._parameter
    }

    setParameter(parameter) {
        this._parameter = parameter
    }
}

module.exports = SimpleClass
const IOC = require('sapphire-ioc')
const SimpleClass = require('./SimpleClass.js')

let ioc = new IOC()
ioc.bind(SimpleClass, () => (
    new SimpleClass(5)
))
const simpleClass = ioc.make(SimpleClass)

Register with singleton

const IOC = require('sapphire-ioc')
const SimpleClass = require('./SimpleClass.js')

let ioc = new IOC()
ioc.singleton(SimpleClass, () => (
    new SimpleClass(5)
))
const simpleClass = ioc.make(SimpleClass)
const simpleClass2 = ioc.make(SimpleClass)

simpleClass.setParameter(3)
simpleClass2.getParameter()     // 3

License

MIT

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago