https://github.com/digitaledgeit/service-oracle
A service locator.
https://github.com/digitaledgeit/service-oracle
Last synced: 22 days ago
JSON representation
A service locator.
- Host: GitHub
- URL: https://github.com/digitaledgeit/service-oracle
- Owner: digitaledgeit
- Created: 2015-01-22T10:38:04.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-10-11T00:09:56.000Z (over 10 years ago)
- Last Synced: 2025-02-19T01:34:58.626Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 191 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# service-oracle
[](https://travis-ci.org/digitaledgeit/service-oracle)
[](https://coveralls.io/github/digitaledgeit/service-oracle?branch=master)
A service locator for NodeJS and the browser.
The all-knowing Oracle knows how to construct your services!
## Installation
npm install --save service-oracle
## Usage
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');
## API
### Methods
#### new ServiceOracle()
Create a new service locator.
#### .has(name : string) : boolean
Check whether a service can be located.
#### .new(name : string) : *
Create a new instance of a service.
**Note**: Cannot create a new instance of a service registered with the `.instance()` method.
#### .get(name : string) : *
Get a shared instance of a service.
#### .instance(name : string, value : *) : ServiceOracle
Register a shared instance of a service.
#### .factory(name : string, value : string|function : ServiceOracle
Register a function or module path as a factory for creating services.
#### .locator(locator : ServiceOracle) : ServiceOracle
Register another locator to proxy service location to.
## Changelog
v1.0.0
- changed `.set(name, instance)` to `.instance(name, instance)`
- added missing tests
- updated documentation
## License
The MIT License (MIT)
Copyright (c) 2015 James Newell