Skip to content

Lightweight dependency injection for TypeScript and JavaScript

License

Notifications You must be signed in to change notification settings

leonadler/lightweight-di

Repository files navigation

Lightweight Dependency Injection

npm version travis build status

A dependency injection library for node and the browser, based on Angulars DI. Optimized for use via TypeScript or babel decorators.

Getting started

1. Install the library in your project

npm install --save lightweight-di

2. Import the required decorators from the package

// TypeScript / Babel / Webpack
import { Injectable } from 'lightweight-di';

// Node.js / Browserify
const { Injectable } = require('lightweight-di');

3. Decorate classes with dependencies

@Injectable
class FileLogger {
    constructor(private fs: FileSystem) { }
    log(message: string) {
        fs.appendToFile('app.log', message);
    }
}

4. Bootstrap your application entry point with Injector

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 or the examples on GitHub.

License

MIT