# sharils-di

> Minimal Dependency Injection

Latest version **1.0.2** (published 2016-10-20) · AGPL-3.0+ license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

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

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2016-10-20 |
| First published | 2016-10-16 |
| Weekly downloads | 0 |
| License | AGPL-3.0+ |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | sharils@users.noreply.github.com |
| Maintainers | juddnw |

## Links

- npm: https://www.npmjs.com/package/sharils-di
- Repository: https://github.com/sharils/js-di
- Homepage: https://github.com/sharils/js-di#readme
- Issues: https://github.com/sharils/js-di/issues
- npm.io page: https://npm.io/package/sharils-di

## Dependencies (1)

- [babel-polyfill](https://npm.io/package/babel-polyfill.md) ^6.16.0

## Recent versions

- 1.0.2 (latest) — 2016-10-20
- 1.0.1 — 2016-10-16
- 1.0.0 — 2016-10-16

## README

sharils-di
==========

Minimal Dependency Injection

Example
-------

`` Di.global `` is a singleton of the Di class.

Register your dependency to a di instance.

```js
Di.global.YourDependency = YourDepedency;
Di.global.yourDependency = new YourDependency();
Di.global.yourDependency = (...args) => new YourDependency(...args);
```

Apply a Di instance to your class. It applies to both itself and its prototype.

```js
import Di from 'sharils-di';

class YourClass {}
Di.global(YourClass);
```

Use dependency depending on how it's registered.

```js
import Di from 'sharils-di';

class YourClass {
    static factory() {
        this.yourDependency = new this.di.YourDependency();
        this.yourDependency = this.di.yourDependency;
        this.yourDependency = this.di.yourDependency();
    }

    constructor() {
        this.yourDependency = new this.di.YourDependency();
        this.yourDependency = this.di.yourDependency;
        this.yourDependency = this.di.yourDependency();
    }
}
Di.global(YourClass);
```

Create a Di instance and register services.

```js
const di = new Di({
  YourDependency,
  yourDependency: new YourDependency(),
  yourDependency(...args) {
    return new YourDependency(...args);
  }
});
```

Of course you can still do the old-school way. But it's not recommended.

```js
const di = new Di();
di.YourDependency = YourDepedency;
di.yourDependency = new YourDependency();
di.yourDependency = (...args) => new YourDependency(...args);
```

Test a Di'ed class with a different Di instance.

```js
const di = new Di();
const SubclassYourClass = di.extends(YourClass);

// Test against SubclassYourClass
```

The reason a subclass is created and tested against is so that the original
class isn't modified and this should prevent us from making mistakes, e.g.
forgot to re-apply the original Di instance to the original class.

Apply di to an object is also possible.

```js
const yourObject = {};
Di.global(yourObject);
```

Development
-----------

Run test in Node.js

```sh
npm test -- watch
```

Run test in browser

```sh
npm start
```

License
-------

AGPL-3.0

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