# dject-core

> Dject Core ==========

Latest version **1.2.1** (published 2019-05-24) · MPL license · 0 weekly downloads

## Install

```sh
npm install dject-core
pnpm add dject-core
yarn add dject-core
bun add dject-core
```

## 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.2.1 |
| Published | 2019-05-24 |
| First published | 2018-03-14 |
| Weekly downloads | 0 |
| License | MPL |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | cmstead |

## Links

- npm: https://www.npmjs.com/package/dject-core
- npm.io page: https://npm.io/package/dject-core

## Recent versions

- 1.2.1 (latest) — 2019-05-24
- 1.2.0 — 2019-05-24
- 1.1.1 — 2019-01-29
- 1.1.0 — 2018-03-17
- 1.0.3 — 2018-03-15
- 1.0.2 — 2018-03-15
- 1.0.1 — 2018-03-14

## README

Dject Core
==========

Dject Core is the core dependency injection system for the Dject dependency injection container. This package is not intended to be used as a standalone DI container.  Instead this is built to support the internal needs for the Dject system, allowing the rest of Dject to be built with good separation of concerns.

For more information on the Dject library, please see the following package:

https://www.npmjs.com/package/dject

## API ##

### New container creation ###

To create a new container, simply call the DjectCoreFactory function provided by the JS module:

```javascript
// In node:
const container = require('dject-core')();

// In the browser:
const container = window.djectCoreFactory();
```

### Registering a New Module ###

Module registration requires a name, a function which constructs the module and an array of dependency names (as strings).  You can only register a module factory to a name once. Any subsequent registrations will throw an error. Module registration is chainable.

```javascript
container
    .register('myDependency', () => { foo: 'bar' }, [])
    .register('anotherModule', (myDependency) => { baz: myDependency }, ['myDependency']);

container
    .register('myDependency', () => { foo: 'bar' }, [])
    .register('myDependency', () => { foo: 'bar' }, []); // Throws an error
```

### Building Modules ###

Building a module can be done simply by calling the build method with the module name.  This will construct all dependencies and the top-level module.  Considering the registration example above, build will result in the following:

```javascript
const myModuleInstance = container.build('anotherModule');

// produces the following:

// {
//     baz: {
//         foo: 'bar'
//     }
// }
```

### Overriding Modules ###

Overriding a module is necessary for situations where a module has been registered, but you need to inject something different into the dependency tree.  This is most common in testing scenarios.

```javascript
container
    .register('myDependency', () => { foo: 'bar' }, [])
    .override('myDependency', () => { foo: 'bar' }, []); // Doesn't throw an error
```

## Version History ##

**1.0.0**

Initial release

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