ServiceManager
🔌What is the ServiceManager?
ServiceManager is probably the most basic implementation of dependency injection container for JavaScript.
Quick start
Execute npm install servicemanager
or yarn add servicemanager
to install servicemanager and its dependencies into your project directory.
Usage
Basics (Hooks Way)
To register objects to service manager, create a file/module for your service context:
//// serviceContext.js//; const context = ;
To get objects back from service manager:
//// anotherFile.js//; // get default service contextconst context = ; // returns a new instance for DefaultResourceManagerconst resourceManager = context; // returns a new instance for CustomCacheManagerconst cacheManager = context; // returns the same session manager object that referenced by mySessionManagerconst sessionManager = context;
Alternatively, to get all needed instances at once:
//// anotherFile2.js//; // get default service contextconst context = ; const resourceManager cacheManager sessionManager = context;
...Or, to have them in more promise-friendly way:
//// anotherFile3.js//; // get default service contextconst context = ; context;
*** Note: Service names can be anything including objects, symbols or strings.
Basics (Functional Way)
To register objects to service manager, create a file/module for your service context:
//// serviceContext.js//; const context = ; ;
To get objects back from service manager:
//// anotherFile.js//;; // returns a new instance for DefaultResourceManagerconst resourceManager = ; // returns a new instance for CustomCacheManagerconst cacheManager = ; // returns the same session manager object that referenced by mySessionManagerconst sessionManager = ;
Alternatively, to get all needed instances at once:
//// anotherFile2.js//;; const resourceManager cacheManager sessionManager = ;
...Or, to have them in more promise-friendly way:
//// anotherFile3.js//;; ;
*** Note: Service names can be anything including objects, symbols or strings.
API
ServiceContext.prototype methods
constructor(...definitions: ServiceDefinitions)
get(dependency: any): any
getRange(...dependencies: Array<any>): Array<any>
ensure(dependencies: Array<any>, callback: (...services: Array<any>) => any): Promise<any>
all(): Array<string>
filter(predicate: FilterPredicate): Array<string>
filterByTag(tag: string): Array<string>
Mechanics
Factory services call generator/dependency target each time they are requested, whereas, Singleton services are registered when they are defined.
; const date1 = Symbol'date1';const date2 = Symbol'date2'; const context = ; console; // calls and returns new Date()console; // calls and returns new Date() again,console; // no calls, returns stored date.
Todo List
See GitHub Projects for more.
Requirements
- node.js (https://nodejs.org/)
License
Apache 2.0, for further details, please see LICENSE file
Contributing
See contributors.md
It is publicly open for any contribution. Bugfixes, new features and extra modules are welcome.
- To contribute to code: Fork the repo, push your changes to your fork, and submit a pull request.
- To report a bug: If something does not work, please report it using GitHub Issues.