# micro-di

> Config-based dependency injection container for node.js, inspired by Symfony2 (PHP) and Spring (Java)

Latest version **1.1.16** (published 2014-11-19) · GPLv3 license · 0 weekly downloads

## Install

```sh
npm install micro-di
pnpm add micro-di
yarn add micro-di
bun add micro-di
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.16 |
| Published | 2014-11-19 |
| First published | 2014-09-05 |
| Weekly downloads | 0 |
| License | GPLv3 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Stefan Zerkalica |
| Maintainers | zerkalica |
| Keywords | ioc, di, dependency, injection |

## Links

- npm: https://www.npmjs.com/package/micro-di
- Repository: https://github.com/zerkalica/micro-di
- Issues: https://github.com/zerkalica/micro-di/issues
- npm.io page: https://npm.io/package/micro-di

## Dependencies (4)

- [bluebird](https://npm.io/package/bluebird.md) ^2.3.11
- [traverse](https://npm.io/package/traverse.md) ^0.6.6
- [hyper-config](https://npm.io/package/hyper-config.md) ^0.1.20
- [define-exceptions](https://npm.io/package/define-exceptions.md) ^1.0.6

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 1.1.16 (latest) — 2014-11-19
- 1.1.15 — 2014-11-14
- 1.1.14 — 2014-11-14
- 1.1.13 — 2014-11-13
- 1.1.12 — 2014-11-11
- 1.1.10 — 2014-11-10
- 1.1.9 — 2014-11-07
- 1.1.8 — 2014-11-07
- 1.1.7 — 2014-11-06
- 1.1.6 — 2014-11-06
- 1.1.5 — 2014-11-06
- 1.1.4 — 2014-11-06
- 1.1.3 — 2014-10-31
- 1.1.2 — 2014-10-31
- 1.1.1 — 2014-10-30
- … 26 more at https://npm.io/package/micro-di/versions

## README

# Micro DI

Config-based dependency injection container for node.js, inspired by Symfony2 (PHP) and Spring (Java).

## Features

* based on [hyper-config](https://github.com/zerkalica/hyper-config): config wrapper with merging, references, string macros, tagging, etc.
* supports annotation
* injects to constructor as options object or arguments
* can clone di container with new context (container per requests)

## Config keywords
* @class - reference to class, container creates new instance of prototype
* @factory - reference to factory function, which returns initialized object, container invoke this function and cache result
* ~path - reference, see [hyper-config](https://github.com/zerkalica/hyper-config)
* @inject - constructor injection type: object, arguments, props
* @tags - array of tags, see [hyper-config](https://github.com/zerkalica/hyper-config)

## Usage

```yaml
# ./config/ex2.all.yml
app:
    console-transport:
        @class: ~App.Transport.Console
        @tags: [req]
        prefix: console-prefix
    logger:
        @class: ~App.Logger
        @scope: req
        helper:
            @factory: ~App.Helper
            @inject: arguments
            text: helper text
            value: helper value
        transports:
            console: ~app.console-transport
        prefix: logger-prefix
```

```javascript
var MicroDi = require('micro-di');
var ConfigLoader = require('node-config-loader');

function ConsoleTransport(options) {
    this._prefix = options.prefix;
    this._reqQuery = null;
}

ConsoleTransport.prototype.write = function (message) {
    console.log(this._prefix + ', ' + this._reqQuery + ': ' + message);
};

ConsoleTransport.prototype.setReq = function (req) {
    this._reqQuery = req.query;
};

function Logger(options) {
    this._helper = options.helper;
    this._transports = options.transports;
    this._prefix = options.prefix;
}

Logger.prototype.log = function (message) {
    this._transports.console.write(this._helper(this._prefix + ': ' + message));
};

function Helper(value, text) {
    return function (message) {
        return message + ', helper: ' + value + ', ' + text;
    };
}

var modules = {
    App: {
        Transport: {
            Console: ConsoleTransport
        },
        Logger: Logger,
        Helper: Helper
    }
};


var builder = MicroDi()
    .addConfig(modules);

ConfigLoader({env: 'dev'})
    .addConfigPath(__dirname + '/config')
    .load(function (config) {
        builder.addConfig(config);
    });

var container = builder.getContainer();

var req = {
    query: 'test query'
};
var newContainer = container.setContext('req', {
    req: req
});

var logger = newContainer.get('app.logger');
logger.log('message');
//console-prefix, test query: logger-prefix: message, helper: helper value, helper text
```

---
_Source: https://npm.io/package/micro-di · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
