Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/writetome51/di-factory
A TypeScript/JavaScript tool for instantiating classes that require dependency injection
https://github.com/writetome51/di-factory
class dependency-injection factory instantiation javascript typescript
Last synced: 19 days ago
JSON representation
A TypeScript/JavaScript tool for instantiating classes that require dependency injection
- Host: GitHub
- URL: https://github.com/writetome51/di-factory
- Owner: writetome51
- License: mit
- Created: 2018-09-21T01:28:36.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-04-19T07:02:34.000Z (almost 6 years ago)
- Last Synced: 2024-12-09T21:56:21.610Z (about 1 month ago)
- Topics: class, dependency-injection, factory, instantiation, javascript, typescript
- Language: TypeScript
- Homepage:
- Size: 33.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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;
```