Simple Dependency Injection with ES Decorators
This project is a easy to use little dependency injection framework on top of ES Decorators. The basic idea is to have a easy way to declare dependencies with ES decorators.
Installation
$ npm install --save di-decorators
Usage
Babel Config
Yout need a ES.next transpiler to run di-decorators, in that case Babel.
$ npm install --global babel
Right now to use decorators with Babel you need to configure it with stage 1. Simple add a ".babelrc" file on the root of your project:
{"stage": 1}
For more details on how to use babel: https://babeljs.io
Inject and Instance
import {instance, inject} from 'di-decorators';
import {ClassToBeInjected} from './somePackage';
@inject(ClassToBeInjected)
class MyClass {
constructor(injected) {
this.dependency = injected;
}
}
var myObject = instance(MyClass); // create a instance of MyClass with the dependencies
myObject.dependency; // will be a instance of ClassToBeInjected
You can inject any class, by default a new instance will be created. In the last example the call to "instance(MyClass)" will be the same as "new MyClass(new ClassToBeInjected())".
Singleton
import {instance, inject, singleton} from 'di-decorators';
@singleton
class TheOne {
}
@inject(TheOne)
class A {
constructor(one) {
this.one = one;
}
}
@inject(TheOne)
class B {
constructor(one) {
this.one = one;
}
}
var a = instance(A),
b = instance(B);
a.one === b.one; // will be true, the instance is the same
a.one === instace(TheOne); // also true
If you want to have only one instance of some class you can use the @singleton decorator, that way every time you inject the same instance of the class.
Inheritance
import {instance, inject} from 'di-decorators';
class Hello {
message() {
return "Hello";
}
}
@inject(Hello)
class Super {
constructor(hello) {
this.hello = hello;
}
}
class World {
message() {
return "World";
}
}
@inject(World)
class MyClass extends Super {
constructor(world, hello) {
super(hello);
this.world = world;
}
helloWorld() {
return this.hello.message() + ' ' +
this.world.message() + '!';
}
}
instance(MyClass).helloWorld(); // will be the string 'Hello World!'
So how it works? Basically when you extend a class with dependency injections we add theses dependencies to the current class so you can pass to the super constructor.
The ideal way to do that is using rest parameters, so you can add more dependencies to the super class without worry to change everyone that extends it.
Se the example bellow:
class A {}
class B {}
@inject(A, B)
class Super {
constructor(a, b) {
this.a = a;
this.b = b;
}
}
class C {}
@inject(C)
class MyClass extends Super {
constructor(c, ...args) {
super(...args);
this.c= c;
}
}
Defining your own provider.
import {instance, provide} from 'di-decorators';
class MyClass {
constructor(name) {
this.name = name;
}
}
provide(MyClass).as(function() {
return new MyClass('provider');
});
instance(MyClass).name; // will be the 'provider' string;
You can use provide to define how to create the instance.
See Also
- restful-express declarative way to define Express routers using decorators.
- express-promise-wrapper simple wrapper to to transform promise results into express functions.
Contributing
- Please take the time to star the project if you like it! "npm star di-decorators" and also on github di-decorators.
- Feel free to fork, and if you are planning to add more features please open a issue so we can discuss about.