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

@modul/es6ioc

v1.0.3

Published

ES6 Dependency Injection container

Downloads

13

Readme

es6ioc

ES6 Dependency Injection container

Motivation

  1. Implement DI principles
  2. Testability
  3. Customization by configuration file

Dependency Annotation

Similar to AngularJs 1.x you must to annotate your ES6 classes with $inject property that container knows what types to inject into constructor.

class Provider {
    constructor(connectionString, logger) {
        this.connectionString = connectionString;
        this.logger = logger;
    }
    getData() {
    }
}
Provider.$inject = ['connectionString', 'logger'];

Register

To define your types in container you have to use 'registerType' method. Method tells the container instantiate object that requires by concrete injection. To map types container uses string aliases.

Example:

import IoC from ‘ioc.js’

const ioc = new IoC();
ioc.registerType('connectionString', 'http://api.com');
ioc.registerType('logger', Logger);
ioc.registerType('provider', Provider);

Resolve

To instatiate concrete object and it's dependencies you have to use 'resolve' method.

let logic = ioc.resolve('logic');

Note that object do not instantiated directly, rather you just say: "Hey, ioc give me the 'logic'". And container first construct object dependencies and then passes them to required object constructor.

Цели

  1. Реализация принципов DI
  2. Повышение тестируемости кода
  3. Возможность конфигурирования приложения

Опеределение зависимостей

Также как в AngularJs 1.x вам нужно определить статическое свойство $inject. Это позволить IoC контейнеру определить какие зависимости требует ваш класс.

class Provider {
    constructor(connectionString, logger) {
        this.connectionString = connectionString;
        this.logger = logger;
    }
    getData() {
    }
}
Provider.$inject = ['connectionString', 'logger'];

Регистрация зависимостей

Первым делом необходиму указать контейнеру соотвествие имени зависимости и класса реализации.

Пример:

import IoC from ‘ioc.js’

const ioc = new IoC();

ioc.registerType('logger', Logger);

Создание экземпляров объектов

Для создания конкретного объекта необходимо вызвать метод resolve

let logic = ioc.resolve('logic');

Обратите внимание, что явного создания зависимостей не происходит. Вы просто говорите контейнеру: "Создай мне зависимость logic". При этом сначала будут созданы все зависимости, а затем сам запрашиваемый объект.