catberry-locator

Service Locator component for Catberry Framework

Service Locator for Catberry Framework

Entire architecture of Catberry Framework is based on Service Locator pattern and Dependency Injection. It means there is only one service locator in a Catberry application and all modules are resolved from this locator when you use getMiddleware method in server.js or startWhenReady in browser.js. Before that moment feel free to register your own modules-services to inject them into Catberry modules via DI.

In Catberry definition of type is just a string used like an argument name in constructors following $ character.

For example your Catberry module's constructor can look like this:

function Constructor($logger$uhrsomeConfigValue) {
    // here logger and uhr instances will be accessible 
    // via dependency injection from service locator 
    // someConfigValue will be accessible from startup config object 
    // via dependency injection too 
}

Catberry's Service Locator implementation has following methods:

/**
 * Registers new type in service locator.
 * @param {string} type Type name, which will be alias in other constructors.
 * @param {Function} constructor Constructor which
 * initializes instance of specified type.
 * @param {Object?} parameters Set of named parameters
 * which will be also injected.
 * @param {boolean?} isSingleton If true every resolve will return
 * the same instance.
 */
ServiceLocator.prototype.register = function (typeconstructorparametersisSingleton){ }
 
/**
 * Registers single instance for specified type.
 * @param {string} type Type name.
 * @param {Object} instance Instance to register.
 */
ServiceLocator.prototype.registerInstance = function (typeinstance) { }
 
/**
 * Resolves last registered implementation by type name
 * including all its dependencies recursively.
 * @param {string} type Type name.
 * @returns {Object} Instance of specified type.
 */
ServiceLocator.prototype.resolve = function (type) { }
 
/**
 * Resolves all registered implementations by type name
 * including all dependencies recursively.
 * @param {string} type Type name.
 * @returns {Array} Array of instances specified type.
 */
ServiceLocator.prototype.resolveAll = function (type) { }
 
/**
 * Resolves instance of specified constructor including dependencies.
 * @param {Function} constructor Constructor for instance creation.
 * @param {Object?} parameters Set of its parameters values.
 * @returns {Object} Instance of specified constructor.
 */
ServiceLocator.prototype.resolveInstance = function (constructorparameters) { }
 
/**
 * Unregisters all registrations of specified type.
 * @param {string} type Type name.
 */
ServiceLocator.prototype.unregister = function (type) { }

This example demonstrates how to use Service Locator in Catberry Framework.

Using in browser.js script:

var RestApiClient = require('./lib/RestApiClient'),
// create catberry application instance. 
    catberry = require('catberry'),
    config = require('./browser-config'),
    cat = catberry.create(config);
 
// then you can register your components to inject into catberry modules. 
cat.locator.register('restApiClient', RestApiClient, config, true);
 
// you can register services only before method cat.startWhenReady() 
// tell catberry to start when HTML document will be ready 
cat.startWhenReady();
 

Using in server.js script:

var catberry = require('catberry'),
    RestApiClient = require('./lib/RestApiClient'),
    connect = require('connect'),
    config = require('./server-config'),
    cat = catberry.create(config),
    app = connect();
 
// when you have created instance of Catberry application 
// you can register in Service Locator everything you want. 
cat.locator.register('restApiClient', RestApiClient, config, true);
 
// you can register services only before method cat.getMiddleware() 
app.use(cat.getMiddleware());
app.use(connect.errorHandler());
http
    .createServer(app)
    .listen(config.server.port || 3000);
 

There are a lot of ways to contribute:

Denis Rechkunov denis.rechkunov@gmail.com