https://github.com/dancecoder/di-service
Really simple dependency injection solution for JavaScript
https://github.com/dancecoder/di-service
Last synced: 5 months ago
JSON representation
Really simple dependency injection solution for JavaScript
- Host: GitHub
- URL: https://github.com/dancecoder/di-service
- Owner: dancecoder
- License: mit
- Created: 2022-07-05T17:36:35.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-25T13:14:19.000Z (almost 3 years ago)
- Last Synced: 2025-09-23T16:47:03.160Z (5 months ago)
- Language: JavaScript
- Size: 53.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# DI Service
Really simple dependency injection solution for JavaScript
## Features
* Implements DI pattern
* Do not impose any system architecture, may be introduced to any JavaScript project (FE and BE both)
* Pure JavaScript implementation (+ TypeScript declaration file)
* Zero dependencies
* ES Module and CommonJS hybrid module
## Example
Declare any class
```javascript
class Settings {
dbServer = process.ENV.DB_SERVER;
dbUser = process.ENV.DB_USER;
dbPassword = process.ENV.DB_PASSWORD; // don't do like this
}
module.exports = { Settings };
```
Use the class as a dependency
```javascript
const { Settings } = require('./settings');
const { SERVICE_REQUIRE } = require('di-service');
class DBConnection {
static [SERVICE_REQUIRE] = [Settings];
constructor(settings) {
// settings -> instance of the Settings class
this.settings = settings;
}
}
module.exports = { DBConnection };
```
Get service instance
```javascript
const { DIService } = require('di-service');
const { DBConnection } = require('./db-connection');
const services = new DIService();
const connection1 = await services.getInstance(DBConnection);
const connection2 = await services.getInstance(DBConnection);
console.log(connection1 === connection2); // -> true
console.log(connection1.settings === connection2.settings); // -> true
console.log(connection1.settings.constructor.name); // -> Settings
```
## Used by
[
](https://bllink.co/en/)