A service locator for NodeJS and the browser.
The all-knowing Oracle knows how to construct your services!
npm install --save service-oracle
var locator = require('service-oracle');
//setup your services
var services = locator()
.instance('Config', require('./config.json'))
.factory('Database', function(locator) {
var config = locator.get('Config');
var database = new Database(config.db);
return database;
})
.factory('Users', function(locator) {
var database = locator.get('Database');
var users = new Users(database);
return users;
})
.factory('Posts', function(locator) {
var database = locator.get('Database');
var posts = new Posts(database);
return posts;
})
.factory('BlogService', function(locator) {
var users = locator.get('Users');
var posts = locator.get('Posts');
var service = new BlogService(users, posts);
return service;
})
;
//somewhere else e.g. in your express routes
var blog = services.get('BlogService');
Create a new service locator.
Check whether a service can be located.
Create a new instance of a service.
Note: Cannot create a new instance of a service registered with the .instance()
method.
Get a shared instance of a service.
Register a shared instance of a service.
Register a function or module path as a factory for creating services.
Register another locator to proxy service location to.
v1.0.0
- changed
.set(name, instance)
to.instance(name, instance)
- added missing tests
- updated documentation
The MIT License (MIT)
Copyright (c) 2015 James Newell