dependency-container-js
TypeScript icon, indicating that this package has built-in type declarations

2.0.0-beta1 • Public • Published

dependency-container-js v2.0.0-beta1

"dependency-container-js" is a simple IOC container pattern for NodeJS.

You can get the latest release from the official npmjs.com feed or from the github releases page.

Build Status

Getting Started

Importing the module and initializing an instance

// Import the module.
const { DependencyContainer } = require('dependency-container-js');
 
// Initialize an instance
const diContainer = new DependencyContainer();

Registering services.

// Registering a service as singleton.
diContainer.singleton('DummyService', () => new DummyService());
 
// Registering a service as transient.
diContainer.transient('DummyService', () => new DummyService());
 

Injecting IoC container to components.

// Register component named 'DummyController'.
diContainer.transient('DummyController', ioc => new DummyController(ioc));
 

Resolving a service

// Resolve service named 'DummyController'.
const dummyController = diContainer.resolve('DummyController');
 

Full demonstration:

Content of DummyService.js:

/**
 * DummyService is a dummy service to demonstrate how to register a service for an IoC
 * container.
 * @class DummyService
 */
class DummyService {
 
    /**
     * Gets the necessary message.
     */
    getMessage(){
        return 'Hello there!';
    }
}
 
module.exports = DummyService;

Content of DummyController.js:

/**
 * DummyController is a dummy component to demonstrate how to resolve
 * registered dependencies.
 * @class DummyController
 */
class DummyController {
    
    /**
     * Initializes an instance of the DummyController.
     * @param {DependencyContainer} ioc - Instance of the IoC container (Mandatory).
     * @param {object} options - Initialization options (Optional).
     */
    constructor(ioc, options){
 
        // Resolve necessary dependencies.
        this.__dummyService = ioc.resolve('DummyService');
        // this.__myOtherLovelyService = ioc.resolve('OtherLovelyService');
 
        // Assign options to the local field.
        this.__options = options;
    }
 
    /**
     * Writes the necessary message to the console.
     */
    saySomething(){
        console.log(this.__dummyService.getMessage());
    }
}
 
module.exports = DummyController;

Content of index.js

// Import the module.
const { DependencyContainer } = require('dependency-container-js');
 
// Import dependent modules.
const DummyController = require('./DummyController');
const DummyService = require('./DummyService');
 
// Initialize an instance of the DIController.
const diContainer = new DependencyContainer();
 
// Register.
diContainer.transient('DummyService', () => new DummyService())
    .transient('DummyController', ioc => new DummyController(ioc))
 
 
// Consider, this is another block of code from another js module.
const controller = diContainer.resolve('DummyController');
controller.saySomething();

Versioning

Used SemVer for versioning. For the versions available, see the tags on this repository.

Authors

License

This project is licensed under the MIT Licece - see the LICENSE.md file for details.

NOTICE: This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Package Sidebar

Install

npm i dependency-container-js

Weekly Downloads

78

Version

2.0.0-beta1

License

MIT

Unpacked Size

29 kB

Total Files

21

Last publish

Collaborators

  • denizkanmaz