Giter Club home page Giter Club logo

node-dependency-injection's Introduction

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 Dependencies DevDependencies 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')

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)
loader.load('/path/to/file.yml')

And get services from your container easily

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

List of features

  • 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

node-dependency-injection's People

Contributors

zazoomauro avatar dornasportse avatar dependabot[bot] avatar barinbritva avatar rafal-ksiazek-rmtcfm-com avatar vilasmaciel avatar madhug-nadig avatar snyk-bot avatar carlino3 avatar avdg avatar cjuega avatar clement-michelet avatar rexuswolf avatar rsaladocid avatar stevearm 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.