https://github.com/leonadler/lightweight-di
Lightweight dependency injection for TypeScript and JavaScript
https://github.com/leonadler/lightweight-di
dependency-injection dependency-tree node typescript-design-patterns typescript-di typescript-library
Last synced: 10 months ago
JSON representation
Lightweight dependency injection for TypeScript and JavaScript
- Host: GitHub
- URL: https://github.com/leonadler/lightweight-di
- Owner: leonadler
- License: mit
- Created: 2017-06-21T21:38:07.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-09-03T19:44:49.000Z (almost 2 years ago)
- Last Synced: 2025-05-27T05:07:37.876Z (about 1 year ago)
- Topics: dependency-injection, dependency-tree, node, typescript-design-patterns, typescript-di, typescript-library
- Language: TypeScript
- Homepage:
- Size: 149 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Lightweight Dependency Injection
[](https://badge.fury.io/js/lightweight-di)
[](https://travis-ci.org/leonadler/lightweight-di/)
A dependency injection library for node and the browser, based on [Angulars DI](https://angular.io/guide/architecture#dependency-injection).
Optimized for use via [TypeScript](https://github.com/Microsoft/TypeScript#readme) or [babel](https://github.com/babel/babel#readme) [decorators](https://babeljs.io/docs/plugins/transform-decorators/).
## Getting started
### 1. Install the library in your project
```sh
npm install --save lightweight-di
```
### 2. Import the required decorators from the package
```typescript
// TypeScript / Babel / Webpack
import { Injectable } from 'lightweight-di';
// Node.js / Browserify
const { Injectable } = require('lightweight-di');
```
### 3. Decorate classes with dependencies
```typescript
@Injectable
class FileLogger {
constructor(private fs: FileSystem) { }
log(message: string) {
fs.appendToFile('app.log', message);
}
}
```
### 4. Bootstrap your application entry point with `Injector`
```typescript
import { Injector } from 'lightweight-di';
const injector = Injector.resolveAndCreate([App, Dependency1, Dependency2]);
injector.get(App).run();
```
## Documentation & Examples
For the full API check out the [documentation](https://github.com/leonadler/lightweight-di/blob/master/docs/api.md)
or the [examples](https://github.com/leonadler/lightweight-di/blob/master/docs/examples) on GitHub.
## License
[MIT](https://github.com/leonadler/lightweight-di/blob/master/LICENSE)