All Projects → mgechev → Injection Js

mgechev / Injection Js

Dependency injection library for JavaScript and TypeScript in 5.1K. It is an extraction of the Angular's ReflectiveInjector which means that it's well designed, feature complete, fast, reliable and well tested.

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Injection Js

Fastify Decorators
Set of Typescript decorators to build Fastify server with controllers, services and hooks
Stars: ✭ 85 (-91.16%)
Mutual labels:  decorators, dependency-injection
React Ioc
Hierarchical Dependency Injection with new React 16 Context API
Stars: ✭ 133 (-86.17%)
Mutual labels:  decorators, dependency-injection
Bottlejs
A powerful dependency injection micro container for JavaScript applications
Stars: ✭ 1,171 (+21.73%)
Mutual labels:  decorators, dependency-injection
Ioc
🦄 lightweight (<1kb) inversion of control javascript library for dependency injection written in typescript
Stars: ✭ 171 (-82.22%)
Mutual labels:  decorators, dependency-injection
resty
A Node.js framework
Stars: ✭ 20 (-97.92%)
Mutual labels:  dependency-injection, decorators
common-injector
Heavily influenced by Angular and it's dependency injection. Inspired by Angular and Indiv.
Stars: ✭ 18 (-98.13%)
Mutual labels:  dependency-injection, decorators
Tsed
📐 Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.
Stars: ✭ 1,941 (+101.77%)
Mutual labels:  decorators, dependency-injection
Testdeck
Object oriented testing
Stars: ✭ 206 (-78.59%)
Mutual labels:  decorators, dependency-injection
tsed
📐 Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.
Stars: ✭ 2,350 (+144.28%)
Mutual labels:  dependency-injection, decorators
vue3-oop
使用类和依赖注入写vue组件
Stars: ✭ 90 (-90.64%)
Mutual labels:  dependency-injection, decorators
Typescript Ioc
A Lightweight annotation-based dependency injection container for typescript.
Stars: ✭ 427 (-55.61%)
Mutual labels:  decorators, dependency-injection
Alki
Dependency Injection for Ruby
Stars: ✭ 27 (-97.19%)
Mutual labels:  dependency-injection
Laravelyaml
Laravel ServiceProvider for using YAML configuration files
Stars: ✭ 8 (-99.17%)
Mutual labels:  dependency-injection
Anakin
This Node module maps all application's dependencies and centralizes everything, at only one place
Stars: ✭ 8 (-99.17%)
Mutual labels:  dependency-injection
Decorum
Tasteful decorators for Ruby.
Stars: ✭ 7 (-99.27%)
Mutual labels:  decorators
Cookbook
🎶 Cookbook for Nette Framework (@nette) & Contributte (@contributte). Read it while its HOT!
Stars: ✭ 30 (-96.88%)
Mutual labels:  dependency-injection
Mydi
moved to https://github.com/cekta/di
Stars: ✭ 21 (-97.82%)
Mutual labels:  dependency-injection
Lld Parking Lot
Repository for low level design solution of parking lot
Stars: ✭ 27 (-97.19%)
Mutual labels:  dependency-injection
Snowball
Android Clean Code Sample Project
Stars: ✭ 26 (-97.3%)
Mutual labels:  dependency-injection
Swiftresolver
Dependency injection framework for Swift
Stars: ✭ 26 (-97.3%)
Mutual labels:  dependency-injection

Build Status Downloads

Dependency Injection

Dependency injection library for JavaScript and TypeScript in 5.2K. It is an extraction of the Angular's dependency injection which means that it's feature complete, fast, reliable and well tested.

Why not Angular version 5 and above?

Angular version 5 deprecated the ReflectiveInjector API and introduced StaticInjector. In short, the dependency injection in the newest versions of Angular will happen entirely compile-time so reflection will not be necessary.

However, if you want to use dependency injection in your Node.js, Vue, React, Vanilla JS, TypeScript, etc. application you won't be able to take advantage of StaticInjector the way that Angular will because your application won't be compatible with Angular compiler.

This means that if you need dependency injection outside of Angular @angular/core is not an option. In such case, use injection-js for fast, small, reliable, high-quality, well designed and well tested solution.

How to use?

$ npm i injection-js
# OR
$ yarn add injection-js

Note:

For ES5 Class syntax and TypeScript you need a polyfill for the Reflect API. You can use:

Also for TypeScript you will need to enable experimentalDecorators and emitDecoratorMetadata flags within your tsconfig.json

TypeScript

import 'reflect-metadata';
import { ReflectiveInjector, Injectable, Injector } from 'injection-js';

class Http {}

@Injectable()
class Service {
  constructor(private http: Http) {}
}

@Injectable()
class Service2 {
  constructor(private injector: Injector) {}

  getService(): void {
    console.log(this.injector.get(Service) instanceof Service);
  }

  createChildInjector(): void {
    const childInjector = ReflectiveInjector.resolveAndCreate([Service], this.injector);
  }
}

const injector = ReflectiveInjector.resolveAndCreate([Service, Http]);

console.log(injector.get(Service) instanceof Service);

ES6

const { Inject, ReflectiveInjector } = require('injection-js');

class Http {}

class Service {
  static get parameters() {
    return [new Inject(Http)];
  }

  constructor(http) {
    this.http = http;
  }
}

const injector = ReflectiveInjector.resolveAndCreate([Http, Service]);

console.log(injector.get(Service) instanceof Service);

ES5

require('reflect-metadata');
var di = require('injection-js');

var Http = di.Class({
  constructor: function() {},
});

var Service = di.Class({
  constructor: [
    Http,
    function(http) {
      this.http = http;
    },
  ],
});

var injector = di.ReflectiveInjector.resolveAndCreate([Http, Service]);

console.log(injector.get(Service) instanceof Service);

API

For full documentation check Angular DI docs:

Ecosystem

This is a list of libraries that are using injection-js. If you have a suggestion on what to add, please don't hesitate to submit a PR.

Libraries

  • ng-packagr Transpile your libraries to Angular Package Format. Part of the official Angular CLI.
  • @martin_hotell/axios-http Injectable axios HttpClient wrapper for browser and node
  • @martin_hotell/rea-di Dependency injection for React done right. Hierarchical injection on both component and service layer powered by injection-js (Angular DI framework) 🖖
  • rxstack RxStack is a realtime object-oriented framework which helps you build a micro service web applications on top of other frameworks like express and socketio by adding an abstraction layer.
  • ServeRX-ts Experimental Node.js HTTP framework using RxJS, built with TypeScript and optimized for serverless deployments. Features declarative routes and dependency injection powered by injection-js.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].