# @writetome51/object-factory

> A simple dependency-injection tool

Latest version **1.0.2** (published 2018-09-21) · MIT license · 0 weekly downloads

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

## Install

```sh
npm install @writetome51/object-factory
pnpm add @writetome51/object-factory
yarn add @writetome51/object-factory
bun add @writetome51/object-factory
```

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2018-09-21 |
| First published | 2018-09-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 12.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Steve Thompson |
| Maintainers | writetome51 |
| Keywords | dependency-injection, dependency, injection, factory |

## Links

- npm: https://www.npmjs.com/package/@writetome51/object-factory
- Repository: https://github.com/writetome51/ObjectFactory
- Homepage: https://github.com/writetome51/ObjectFactory#readme
- Issues: https://github.com/writetome51/ObjectFactory/issues
- npm.io page: https://npm.io/package/@writetome51/object-factory

## Dependencies (1)

- [basic-data-handling](https://npm.io/package/basic-data-handling.md) latest

## Recent versions

- 1.0.2 (latest) — 2018-09-21
- 1.0.1 — 2018-09-21
- 1.0.0 — 2018-09-21

## README

# DIFactory

DIFactory makes it easy to instantiate a class whose constructor uses  
dependency injection.

To make DIFactory handle dependency-injection for a class, first you  
need to register that class with DIFactory.  In this example, a class  
is registered right below its own definition:
```
export class ExampleClass{

   constructor(
        // begin injected dependencies...
        blueCar: BlueCar,
        greenCar: GreenCar,
        redCar: RedCar,
        // end injected dependencies
        
        // additional arguments:
        name: string,
        age: number
    ){ ... }

   ...code...

} // Below the closing brace, ExampleClass is registered with DIFactory:

 DIFactory.register(
     {
        class: ExampleClass,

        // Make sure the dependencies are listed in same order as they are 
        // in the constructor parameters above:
        dependencies: [BlueCar, GreenCar, RedCar]
     }
 );
 ```
 And that's it.  Now that it's registered, you can get an instance of  
 ExampleClass:
```
// for the name and age arguments in the constructor, we're passing 'Steve' and 30:
let instance = DIFactory.getInstance(ExampleClass, ['Steve', 30]);
 ```
 In the statement above, the array argument is there to hold any additional  
 arguments that get passed to ExampleClass' constructor after the injected  
 dependencies.   
 (If there are no arguments after the dependencies, it's unnecessary to pass the array.)


## Installation

You must have npm installed first.  Then, in the command line:

```bash
npm install @writetome51/di-factory
```

## Loading
```
// If using TypeScript:
import { DIFactory } from '@writetome51/di-factory';
// If using ES5 JavaScript:
var DIFactory = require('@writetome51/di-factory').DIFactory;
```

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