npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

di-decorators

v0.2.2

Published

Simple Dependency Injection with ES Decorators

Downloads

28

Readme

Simple Dependency Injection with ES Decorators

Build Status Coverage Status npm version Code Climate npm Join the chat at https://gitter.im/lgvo/di-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.

Please take the time to star the project if you like it! "npm star di-decorators" and also on github di-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())".

Immutable

import {instance, immutable} from 'di-decorators';

@immutable
class CannotChange {
    constructor() {
        this.value = 1;
    }
}

var obj = instance(CannotChange);

obj.value; // 1
Object.isFrozen(obj); // true

obj.value = 2; // Throws a TypeError: Cannot assign to read only property 'value'

You can mark a class as immutable so the instance will be frozen.

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

  • Pull Requests are welcome!
  • Feel free to fork, and if you are planning to add more features please open a issue so we can discuss about.
  • Mind the test coverage and code complexity, if you can do it with TDD probably will come with both.
  • Try to use only ES5 in the core lib.

License

MIT