1.0.1 • Published 3 years ago

@next-generation-node/ioc-container v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

IoC-container

An easy-to-use and powerful container for dependency inversions in Node.JS

Examples

'use strict';

const {DIContainer} = require('../');

//init names/interfaces
const IService1 = Symbol('IService1');
const IService2 = Symbol('IService2');
const IService3 = Symbol('IService3');

//init providers
const provider1 = {
  imports: [],
  factory: () => {
    console.dir(`${1} : ${'nothing'}`);
    return 1;
  },
};

const provider2 = {
  imports: [IService1],
  factory: ({[IService1]: service1}) => {
    console.dir(`${2} : ${service1}`);
    return 2;
  },
};

const provider3 = {
  imports: [IService1, IService2],
  factory: ({[IService1]: service1, [IService2]: service2}) => {
    console.dir(`For transient example: ${Date.now()}`);
    console.dir(`${3} : ${[service1, service2]}`);
    return 3;
  },
};

const container = new DIContainer()
  .addSingleton(IService1, provider1)
  .addSingleton(IService2, provider2)
  .addTransient(IService3, provider3)
  .build();

const {[IService1]: s1, [IService2]: s2, [IService3]: s3} = container;
console.dir({s1, s2, s3});

//transient example
//The factory is called every time a dependency is accessed
setTimeout(() => {
  const {[IService3]: s3_2} = container;
  return s3_2;
}, 1000);

IoC Container 1.0.0:

  • registering dependencies based on providers
  • supported singleton/transient dependencies
  • graph-based dependency building
  • checking dependencies for cyclicity