0.0.5 • Published 2 years ago
short-di v0.0.5
short-di
Short di loading from config tool. Used to loosen coupling between js modules, and realization Dependency-inversion principle.
Installation
For installation type npm command
npm i short-di --save
Usage
- Require or import short-di package like:
const shortDi = require('short-di'); //for JS
import * as shortDi from 'short-di'; //for TS
- For creating variative loading point use "load(<moduleAbstractID>, <currentModule>)" function:
const usersRepo = shortDi.load("IUsersRepository", module); //its JS.
//for TS you will need create your own interface of needle module and implement it.
//Create runtime checking of loaded class should you own too
- in directory with main script create json config named like "<scriptName>.shortdi.json":
//files example:
...
index.js
index.shortdi.json
- In this json file add modules thats should be loaded instead of abstract identifiers. For example:
//index.shortdi.json
{
"IUserRepository": "../repositories/mysql/mysql-user-repository"
}
- Short-di package will load this modules instead abstract-module-ids you specified in "load()" function.
In other words, with config:
//index.shortdi.json
{
"ICitiesParser": "some-path-to-cities-parser-module"
}
The code:
const cityParser = shortDi.load("ICitiesParser", module);
Will works like:
const cityParser = require("some-path-to-cities-parser-module");
Notes
- json short-di config file should be near main running script, and should contains resolvings for all "loads" in programm (includes required scripts).
- json short-di config file should be names like ".shortdi.json".
- The realization and mechanics had been full reworked from v0.2, and became race-condition-safety. Now may be used in asynchronous functions and asynchronous-uses-frameworks like "mocha".
- To avoid repetition of module identifiers, try to use as detailed module identifiers as possible.
- When loading, program will run from passed module by parents, checks existance of ".shortdi.json"-like di-config-files, and uses the most-close-to-main-script di-config-file.
For example:
if module-parents cache looks like:
+ main-script (has .shortdi.json file)
+ first-required-script (has .shortdi.json file)
+ script-where-used-shortdi-load (has not .shortdi.json file)
then will be used config of main-script, case it most close to main script.
Advanced example
//file-system
main-script.js
main-script.shortdi.json
+catalog1
|-connected-module1-with-loads.js
|-+included-catalog
| |-loaded-module-1.js
+catalog2
|-connected-module1-with-loads.js
|-+included-catalog
| |-loaded-module-2.js
...
//main-script.shortdi.json
{
"someModule1": "./included-catalog/loaded-module-1",
"someModule2": "./included-catalog/loaded-module-2",
}
//connected-module1-with-loads.js
const shortDi = require('short-di');
const someModule = shortDi.load("someModule1", module)
...
//connected-module2-with-loads.js
const shortDi = require('short-di');
const someModule = shortDi.load("someModule2", module)
...
//main-script.js
const shortDi = require('short-di');
const connectedModule1 = require('./catalog1/connected-module1-with-loads')
const connectedModule1 = require('./catalog2/connected-module2-with-loads')
...
Recipes
- Uses shortDi loadings in some module.
- Add configuration for it in unit-tests with loading dependency-stubs
- In main program code resolve in di-config real dependencies it should use
Author
- Anatoly Starodubstev "Pantagruel74"
- Tostar74@mail.ru
License
- MIT