instance-locator
v0.0.2
Published
Solution to resolve problems related with dependency inversion principles.
Downloads
1
Readme
The library helps to build your own module system. Barely it resolves Dependency Inversion Principle of SOLID. It is used as barebone for an application that simplify logic integration and its usage.
Be free and ask me questions personally on Gitter
Basic usage
Register basic models for future instance.
const { ContainerLocator } = require('instance-licator')
const config = {
test: 'configTest'
}
class CoreModel {
constructor(config) {
this.config = config
}
test() { return 'coreTest' }
}
class DbModel {
constructor(core) {
this.core = core
}
test() {
return 'dbTest'
}
}
Define symbols for instances
const TYPES = {
config: Symbol('config'),
core: Symbol('core'),
db: Symbol('db')
}
Create locator and register models
const locator = new ContainerLocator()
locator
.registerValue(TYPES.config, config)
.register(TYPES.core, CoreModel, [ TYPES.config ])
.register(TYPES.db, DbModel, [TYPES.core])
Initiate models by the locator, use your logic.
const instance = locator.get(TYPES.db)
const core = locator.get(TYPES.core)
Use your logic
// Outputs dbTest.
console.log(instance.test(), 'dbTest')
// Outputs coreTest.
console.log(instance.core.test())
// Outputs configTest.
console.log(instance.core.config.test)
// Same instances.
console.log('Same instances - ', core == instance.core)
Also check the basic example and tests.