Socket
Socket
Sign inDemoInstall

di-container

Package Overview
Dependencies
1
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    di-container

Dependency injection container for Node.js.


Version published
Weekly downloads
189
increased by19.62%
Maintainers
1
Install size
7.44 kB
Created
Weekly downloads
 

Readme

Source

di-container

Basic Dependency Injection (DI) container for Node.js inspired by code examples found in the book Node.js Design Patterns.

Installation

$ npm install di-container

Usage

Factory functions can simply list their dependencies in the argument list (as made popular by AngularJS). Alternatively a module can be defined as an array of dependency names followed by the factory function. The latter form is more resilient to code minifcation and name mangling.

Example

/* lib/person-factory.js */
module.exports = function(name) {
  return {
    name: name
  };
}

/* lib/animal-factory.js */
module.exports = ['type', function(a) {
  return {
    type: a
  };
}]

/* index.js */
var DiContainer = require('di-container');
var diContainer = new DiContainer();

diContainer.register('name', 'alice');
diContainer.factory('person', require('./lib/person-factory'));

var person = diContainer.get('person');
// person.name == 'alice'

diContainer.register('type', 'dog');
diContainer.factory('animal', require('./lib/animal-factory'));

var animal = diContainer.get('animal');
// animal.type == 'dog'

License

MIT

Keywords

FAQs

Last updated on 09 Feb 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc