Giter Site home page Giter Site logo

northern / di.js Goto Github PK

View Code? Open in Web Editor NEW
1.0 4.0 1.0 291 KB

An easy to use Dependency Injection Container for JavaScript & TypeScript

Home Page: https://www.npmjs.com/package/@northern/di

License: MIT License

JavaScript 3.38% TypeScript 96.62%
dependency-injection inversion-of-control di-container javascript typescript container

di.js's Introduction

DI

A simple Dependency Injection container for JavaScript & TypeScript.

Install

Either use npm:

npm install @northern/di

or Yarn:

yarn add @northern/di

Introduction

To use DI in your application simply import (or require) the Container and create a Container instance:

import Container from '@northern/di'

const container = new Container()

With the newly instantiated container, you can start registering services. There are two ways you can register a service, either use the service method or you can use the factory method.

Registering a service

With the service method a new service provider can be registered. The service method takes three parameters:

Container.service(name, provider[, lazy = true, overwrite = false])

The name is a string with the "name" of the service, e.g. logger. The provider is a function that, when called, "returns" the service instance. The lazy parameter specifies whether the service is "lazy" or not, more on that later and the overwrite parameter allows you to re-register a service if it already exists in the service registry of the container (handy during integration tests when you want to swap a service with a mock).

A "service" instance is only ever created once (as opposed to a factory, which returns a new instance of the service each time).

Let's look at a simple service registration example:

class Logger {
  info(message) {
    console.info(message)
  }
}

container.service('logger', container => {
  return new Logger()
})

It's that simple. We can now get the "logger" service by using the get method on the container:

const logger = container.get('logger')

logger.info("Hello DI")

Since the Container instance is passed into the service provider, it is possible to "wire" multiple services together (i.e. create service dependencies by having one service require another already registered service). A service provider can use already registered services and "inject" them into other services. E.g. if we register a service then we can pass an instance of the logger service into that other service:

class PaymentService {
  constructor(logger) {
    this.logger = logger
  }

  processPayment(paymentObject) {
    this.logger.info("Starting payment process.")
  }
}

container.service('paymentService', container => {
  const logger = container.get('logger')

  const service = new PaymentService(logger)

  return service
})

const paymentService = container.get('paymentService')
paymentService.processPayment({ ... })

Lazy services

When a service is not "lazy" then the instance of that service will be created the moment the service is registered (i.e. when the service method is called). This is not usually desireable (and why lazy by default is true). E.g. if the dependency graph is large and not all services are always used, i.e. usage depends of the code path of the application, it might be better to instantiate a service on a need by need basis. This what a "lazy" service does; the instance is created the first time the service is requested rather than when the service was registered.

Registering a factory

With the factory method a new factory provider can be created. A factory will always return a new instance each time it is requested from the container. The factory registration is the same as that of a service except that a factory cannot be lazy (i.e. its always lazy).

The factory method only has three parameters:

Container.factory(name, provider[, overwrite = false])

That's it. Have fun wiring!

di.js's People

Contributors

northern avatar dependabot[bot] avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar Amon Ng avatar  avatar

Forkers

peideamonng

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.