Giter VIP home page Giter VIP logo

di-decorators's Introduction

Simple Dependency Injection with ES Decorators

Build Status Coverage Status npm version Code Climate npm

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

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.

License

MIT

di-decorators's People

Contributors

lgvo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.