Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

node-dependency-injection

zazoomauro17.1kMIT3.2.2TypeScript support: included

The NodeDependencyInjection component allows you to standardize and centralize the way objects are constructed in your application.

node, dependency, injection, service, container, console, inversion-of-control, service-locator

readme

Node Dependency Injection

NDI Logo

A special thanks to Symfony which was a great inspiration and example for this project.

The Node Dependency Injection component allows you to standardize and centralize the way objects are constructed in your application.

Npm Version Build Status Publish Status Code Coverage Code Climate Coding Standard Known Vulnerabilities Npm Downloads License

Installation

npm install --save node-dependency-injection

Usage: register and get services

Imagine you have a Mailer class like this:

// services/Mailer.js

export default class Mailer {
  /**
   * @param {ExampleService} exampleService
   */
  constructor(exampleService) {
    this._exampleService = exampleService;
  }

  ...
}

You can register this in the container as a service:

import {ContainerBuilder} from 'node-dependency-injection'
import Mailer from './services/Mailer'
import ExampleService from './services/ExampleService'

let container = new ContainerBuilder()

container
  .register('service.example', ExampleService)

container
  .register('service.mailer', Mailer)
  .addArgument('service.example')

And get services from your container

const mailer = container.get('service.mailer')

Autowire for TypeScript

import {ContainerBuilder, Autowire} from 'node-dependency-injection'

const container = new ContainerBuilder(
  false, 
  '/path/to/src'
)
const autowire = new Autowire(container)
await autowire.process()

or from yaml-json-js configuration

# /path/to/services.yml
services:
  _defaults:
    autowire: true
    rootDir:  "/path/to/src"

You can also get a service from a class definition

import SomeService from '@src/service/SomeService'

container.get(SomeService)

If you are transpiling your Typescript may you need to dump the some kind of service configuration file.

import {ContainerBuilder, Autowire, ServiceFile} from 'node-dependency-injection'

const container = new ContainerBuilder(
  false, 
  '/path/to/src'
)
const autowire = new Autowire(container)
autowire.serviceFile = new ServiceFile('/some/path/to/dist/services.yaml')
await autowire.process()

My proposal for load configuration file in a production environment with transpiling/babel compilation:

if (process.env.NODE_ENV === 'dev') {
  this._container = new ContainerBuilder(false, '/src');
  this._autowire = new Autowire(this._container);
  this._autowire.serviceFile = new ServiceFile('/some/path/to/dist/services.yaml');
  await this._autowire.process();
} else {
  this._container = new ContainerBuilder(false, '/dist');
  this._loader = new YamlFileLoader(this._container);
  await this._loader.load('/some/path/to/dist/services.yaml');
}
await this._container.compile();

Configuration files: how to load and use configuration files

You can also use configuration files to improve your service configuration

# /path/to/file.yml
services:
  service.example:
    class: 'services/ExampleService'

  service.mailer:
    class: 'services/Mailer'
    arguments: ['@service.example']
import {ContainerBuilder, YamlFileLoader} from 'node-dependency-injection'

let container = new ContainerBuilder()
let loader = new YamlFileLoader(container)
await loader.load('/path/to/file.yml')

And get services from your container easily

...
const mailer = container.get('service.mailer')

List of features

  • Autowire for TypeScript
  • Configuration files with JS, YAML or JSON.
  • Multiple configuration files
  • Custom relative service directory
  • Compiling container
    • Custom compiler pass
    • Change definition behaviour
  • Using a factory to create services
  • Nullable Dependencies
  • Public or private services
  • Service Aliasing
  • Service Tagging
  • Parameters Injection
  • Lazy Services
  • Deprecate Services
  • Decorate Services
  • Synthetic Services
  • Non Shared Services
  • Parent and Abstract Services
  • Custom Logger
  • Container as Service

Please read full documentation

ExpressJS Usage

If you are using expressJS and you like Node Dependency Injection Framework then I strongly recommend you to use the node-dependency-injection-express-middleware package. That gives you the possibility to retrieve the container from the request.

npm install --save node-dependency-injection-express-middleware
import NDIMiddleware from 'node-dependency-injection-express-middleware'
import express from 'express'

const app = express()

const options = {serviceFilePath: 'some/path/to/config.yml'}
app.use(new NDIMiddleware(options).middleware())

Express Middleware Documentation

TypeScript Usage

If you are using typescript and you like Node Dependency Injection Framework then typing are now provided at node-dependency-injection so you do not have to create custom typing anymore.

npm install --save node-dependency-injection
import { ContainerBuilder } from 'node-dependency-injection'
import MongoClient from './services/MongoClient'
import { Env } from './EnvType'

export async function boot(container = new ContainerBuilder(), env: Env) {
    container.register('Service.MongoClient', MongoClient).addArgument({
        host: env.HOST,
        port: env.PORT,
    })
}

Resources

changelog

Change Log

[1.11.2] - 2018-01-03

Changed

  • ContainerBuilder refactor
  • FileLoader refactor

[1.11.1] - 2017-12-27

Changed

  • Updating dependencies
  • Updating dev dependencies

[1.11.0] - 2017-09-08

Added

  • append parent arguments to the child service
  • check if the abstract service is well formed

Changed

  • serviceCircularReferenceException thrown when container compile fails with RangeError exception
  • moving from .throws to .throw unit testing
  • updating package with newer compatible dependencies

[1.10.2] - 2017-08-16

Changed

  • Updating dependencies
  • Updating dev dependencies

[1.10.1] - 2017-06-19

Changed

  • fixing changelog

[1.10.0] - 2017-06-15

Added

  • decoration priority
  • allow complex parameter as objects
  • add a parameter in the ContainerBuilder constructor to use the sent logger instance instead of the default console service

    Changed

  • inverting instead of shared false by default instance is shared by default
  • updating major nyc dev dependency package

[1.9.3] - 2017-05-30

Added

  • adding travis node 8 version

    Changed

  • fixing out dating dev dependencies

[1.9.2] - 2017-05-25

Changed

  • fixing ContainerBuilder code issue Method '_getInstance' has a complexity of 10.
  • fixing ContainerBuilder code issue Similar code found in other locations

[1.9.1] - 2017-05-25

Changed

  • fixing fs-extra out to date dependency
  • fixing all the dev out to date dependencies

[1.9.0] - 2017-05-22

Changed

  • refactoring compile optimization
  • refactoring compile removal
  • getInstanceFromDefinition is now public

    Added

  • adding container builder remove method
  • adding container builder isSet method
  • adding add compiler pass priority argument
  • adding decorators

[1.8.2] - 2017-04-24

Changed

  • Fix: FileLoader cannot load files in subfolder

[1.8.1] - 2017-04-10

Changed

  • Removing linkedIn link from README file

[1.8.0] - 2017-04-10

Added

  • Adding definition synthetic parameter
  • Public container direct set method
  • Container get method will only return you a valid instance
  • Remove not necessary instances from container on compile
  • Reference Symfony as a source of inspiration
  • Adding Additional Attributes on Tags
  • Inject Instances into the Container

    Changed

  • Refactor following new standard rules
  • Removing linkedIn link from README file

[1.7.4] - 2017-03-22

Changed

  • Throw an exception if the method call does not exists

[1.7.3] - 2017-03-21

Changed

  • Adding standard coding style configuration

[1.7.2] - 2017-03-21

Changed

  • Add Definition class in to the index file

[1.7.1] - 2017-03-20

Changed

  • Fixing npm version issue

[1.7.0] - 2017-03-20

Added

  • Remove definition container method
  • Managing Configuration with Extensions
  • Controlling the pass ordering
  • Adding code climate badge

[1.6.1] - 2017-03-15

Changed

  • Fixing configuration files Boolean arguments issue
  • Fixing configuration files Boolean parameters issue

[1.6.0] - 2017-03-10

Added

  • Deprecating Services
  • Using a Factory to Create Services
  • Passing Parsed Arguments to the Factory Method
  • Ignoring Missing Dependencies

[1.5.0] - 2017-03-06

Added

  • Adding hasDefinition public container method
  • Adding has container public method
  • Adding getDefinition public container method
  • Adding findDefinition public container method
  • Injecting in to public fields properties
  • Adding Definition lazy service property

    Changed

  • Deprecating second constructor argument of File loader
  • Deprecating not setting first argument on config file load method

[1.4.1] - 2017-02-23

Changed

  • Removing CHANGELOG file description
  • Using npm version script instead or server:release to do npm packages releases

[1.4.0] - 2017-02-20

Added

  • Adding parameters on arguments wrapped on %{string}% with the configuration files
  • Adding imports feature to load automatically more services in another files

[1.3.3] - 2017-02-07

Changed

  • Refactoring container builder preventing having two different container builders for compiled and not compiled containers
  • Fixing definition private get arguments class.
  • Preventing instantiating reference service twice

[1.3.2] - 2017-02-06

Changed

  • Preventing compiling an already compiled container

[1.3.1] - 2017-01-30

Changed

  • Fix findTaggedServiceIds returns the tag name instead of definition

    Added

  • Adding unit testing code coverage tools
  • Adding codecov integration
  • Adding npm downloads badges
  • Adding LICENCE

[1.3.0] - 2017-01-30

Added

  • Register compiler pass from the ContainerBuilder
  • Aliasing: You may sometimes want to use shortcuts to access some services.
  • Tagging: Services configured in your container can also be tagged.

[1.2.2] - 2017-01-23

Changed

  • Fix prevent instantiate class again if we get a service and then compile

[1.2.1] - 2017-01-20

Changed

  • Preventing instantiating service twice

[1.2.0] - 2017-01-20

Added

  • Adding compiled container and frozen container

[1.1.1] - 2017-01-20

Changed

  • Moving configuration service test files to config folder
  • Modifying file exists exception message

[1.1.0] - 2017-01-20

Added

  • Adding method call with arguments feature
  • Overriding the whole arguments collection in the definition model
  • Adding Changelog file
  • Updating README file

Changed

  • Refactoring parsing definitions

[1.0.6] - 2017-01-19

Added

  • Following the default js standard coding standard

Changed

  • Using path join instead of file path constructor

Removed

  • Removing stage-2 babel preset

[1.0.5] - 2017-01-19

Added

  • Adding unit testing for reference and package reference
  • Adding js docs to the file loader abstract class
  • Coding standards on test spec files

Changed

  • File loader class moving from private argument path to filePath
  • Refactoring json file loader
  • Updating README file adding more configuration examples

[1.0.4] - 2017-01-18

Changed

  • Update travis configuration

[1.0.3] - 2017-01-18

Changed

  • Update travis configuration

[1.0.2] - 2017-01-18

Changed

  • Update README file

[1.0.1] - 2017-01-18

Added

  • Adding travis configuration

[1.0.0] - 2017-01-18

Added

  • Initial commit