https://github.com/gobwas/dm.js
Javascript Dependency Injection Manager
https://github.com/gobwas/dm.js
Last synced: about 1 month ago
JSON representation
Javascript Dependency Injection Manager
- Host: GitHub
- URL: https://github.com/gobwas/dm.js
- Owner: gobwas
- License: mit
- Created: 2014-01-28T18:54:13.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2016-07-25T07:55:49.000Z (almost 9 years ago)
- Last Synced: 2025-03-07T14:38:56.265Z (about 2 months ago)
- Language: JavaScript
- Size: 2.9 MB
- Stars: 108
- Watchers: 12
- Forks: 4
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# dm.[js](https://developer.mozilla.org/en/docs/JavaScript)
[](http://badge.fury.io/js/dm)
[](https://travis-ci.org/gobwas/dm.js)
[](https://coveralls.io/r/gobwas/dm.js)> Dependency Injection Manager for javascript.
## Introduction
**dm.js** is a javascript library that implements dependency injection pattern. It could work both in node or browser.
It takes care of asynchronous creating, configuring and injecting objects aka *services* inside of your code.
There is a good chance to keep your application design loose coupled, well structured and flexible with dependency injection pattern.
If you are not familiar with dependency injection pattern, use [wiki](https://github.com/gobwas/dm.js/wiki) to get more theory info.
## Usage
```js
var DM = require("dm"),
Async = require("dm/lib/async/q"),
Loader = require("dm/lib/loader/cjs"),
dm;dm = new DM(new Async(Q), new Loader(require), {
parameters: {
"logs": "/var/log/app/"
},services: {
"logger": {
// this is where the loader must get a constructor for this service
"path": "./src/log/logger/polylogue.js",
// this is what calls dm must do on just created service
"calls": [
// '@' symbol is the link to another service from config
[ "addHandler", ["@handler"] ]
]
},"handler": {
"path": "./src/log/handler/file.js",
// this is what arguments pass to the constructor
"arguments": [
{
// '%' symbol is the getter of parameter from config (defined above)
// curly braces is a kinda simple templating feature (works with '@', '#' and '%')
"path": "%{logs}/log.txt"
}
]
}
}
});dm
.get("logger")
.then(function(logger) {
// now we got instantiated and configured logger
logger.info("Hello! It works!");
});```
## Quick overview
DM can:
+ call constructor of your object with needed arguments;
+ make any calls on created object with any arguments;
+ directly set some properties;
+ pass as dependency another service from configuration, or its property, or its method, or its method call result;
+ pass as dependency some file content (with fs.readFile in cjs, and require(text!) in amd);
+ pass as dependency value of parameters, defined in configuration object;
+ pass as dependency some static string with wildcards, that could be any kind of dependencies above;
+ cache or always create new object;
+ retrieve already created objects and work with them as cached services;
+ be configured on the fly with new definitions of services;
+ use custom factory for the service, to make more complex things;Full syntax definition you can find in the [wiki](https://github.com/gobwas/dm.js/wiki).
## Documentation
There is a [wiki](https://github.com/gobwas/dm.js/wiki) for dive deep with dm usage, syntax and ideology.
Also, you can check the [API docs](https://github.com/gobwas/dm.js/tree/master/doc).## Contributing
All developing version are available to install from npm as `[email protected]`.
To publish new release candidate (rc abbr) just do `npm publish --tag x.y.z-rc` and bump version in package.json `x.y.z-rc0`.## License
[MIT](LICENSE)