Service Locator for Catberry Framework
Description
The entire architecture of the Catberry Framework is based on Service Locator pattern. It means that there is only one Service Locator (module registry) in a Catberry application and all application's and framework's modules are resolved from the locator when you start the application.
In case of Catberry Framework, starting an application means to
call getMiddleware
method in server.js
or startWhenReady
in browser.js
. Before that moment feel free to register your own
modules-services into Service Locator.
Usage
Registering your Implementations
For example, you have a class that implement something:
// constructor always has the only argument – the Service Locator. { thisberry = locator; }
And you might have an "old style" class definition using constructor and prototype:
// constructor always has the only argument – the Service Locator. { // config is always registered in a Catberry application. thisberryType = locatorberryType;}
So, you need to register these implementations into the locator before starting an application.
Registering in browser.js
script:
const catberry = ;const config = ;const cat = catberry; // when you have created an instance of the Catberry application// you can register your modules in the Service Locator.catlocator;catlocator; // you can register services only before the cat.startWhenReady() method is calledcat;
Registering in server.js
script:
const catberry = ;const config = ;const cat = catberry;const connect = ;const http = ;const app = ; // when you have created an instance of the Catberry application// you can register your modules in the Service Locator.catlocator;catlocator; // you can register services only before cat.getMiddleware() method is calledapp;app;http ;
All cat-components and stores in a Catberry application are registered automatically into the Service Locator. So, you don't need to do that on your own.
Using Implementations
As far as every module's constructor has the only argument – the Service Locator,
you can resolve all modules-dependencies while creating new instances of these
modules using locator
argument:
// constructor always has the only argument – the Service Locator. { // you custom modules thisberry = locator; // Catberry's modules thisconfig = locator; thislogger = locator; }
Also, you can share the same instance across all modules in the application:
catlocator;// Berry will be a singletoncatlocator; const cat1 = catlocator;const cat2 = catlocator; console; // true
Or if you have an instance itself instead of its constructor you can do following:
catlocator;// Berry is registered as an instancecatlocator; const cat1 = catlocator;const cat2 = catlocator; console; // true
Another possible options is to register a list of implementations and to resolve all the instances as a list:
catlocator;catlocator;catlocator; const allTheCats = catlocator; console; // trueconsole; // trueconsole; // true
Please keep in mind that the last registered implementation will be the first in the resolved list.
Interface
Catberry's Service Locator implementation has following methods:
/** * Implements a Service Locator pattern. */ /** * Registers a new type name in the service locator. * @param * @param * which creates instances of the specified type name. * @param {boolean?} isSingleton If true then the only instance will * be created on the first "resolve" call and next calls will * return this instance. */ {} /** * Registers a single instance for the specified type. * @param * @param */ {} /** * Resolves the last registered implementation by the type name. * @param * @returns */ {} /** * Resolves all registered implementations by the type name. * @param * @returns */ {} /** * Unregisters all registrations of the specified type name. * @param */ {}
Contributing
There are a lot of ways to contribute:
- Give it a star
- Join the Gitter room and leave a feedback or help with answering users' questions
- Submit a bug or a feature request
- Submit a PR
Denis Rechkunov denis.rechkunov@gmail.com