1.1.0 • Published 2 years ago

bilstrap-iocjs v1.1.0

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

Bilstrap-iocjs

Bilstrap-iocjs is a IoC(Inversion of Controll) container for your typescript project.

Install

npm install bilstrap-iosjs@latest

This is a simple light weight typescript library that let you to remove your hardcodes dependencies from your ES6 classes. This allow you to manage both singleton and other dependency classes.

Oldway

class Piston {
  constructor() {}

  public fire() {
    console.log('piston is firing')
  }
}

class Engine {
   constructor(private piston: Piston) {
     console.log('initializing Engine');
     // hardcodes value
     this.piston = new Piston();
   }

   public start() {
     console.log('Stating the engine');
     this.piston.fire();
   }

   public stop() {
     console.log('Stoping the engine');
   }
 }

 class Car {
   constructor(private engine: Engine) {
     // hardcoded values
     this.engine = new Engine(new Piston());
   }

   public start() {
     this.engine.start();
   }

   public getEngine(): Engine {
     return this.engine;
   }
 }

Using bilstrap-iocjs

import { Container } from 'bilstrap-iocjs';

class Piston {
  constructor() {}

  public fire() {
    console.log('piston is firing')
  }
}

class Engine {
   constructor(private piston: Piston) {
     console.log('initializing Engine');
   }

   public start() {
     console.log('Stating the engine');
     this.piston.fire();
   }

   public stop() {
     console.log('Stoping the engine');
   }
 }

 class Car {
   // you dependencies define here
   constructor(private engine: Engine) {}

   public start() {
     this.engine.start();
   }

   public getEngine(): Engine {
     return this.engine;
   }
 }

const container = new Container();
// here you register the class
container.register('piston', Piston, []);
container.register('engine', Engine, ['piston']);
container.register('car', Car, ['engine']);

const BMW: any = container.get('car');
BMW.start();

Singleton classes

// singleton example
container.register('piston', Piston, [], true);

 const BMW: any = container.get('car');
 const BENZ: any = container.get('car');
 BMW.start();
 BENZ.start();

 console.log('is same engine: ', BMW.getEngine() === BENZ.getEngine()) // false
 console.log('is same Piston: ', BMW.getEngine().getPiston() === BENZ.getEngine().getPiston()) // true

Descorators

On the way

Features

register(uniqueClassName, BaseClass, Dependencies, singletonOrNot): void

PrameterDescription
name : stringunique name to identify the class
definition: ClassClass that you want to register
dependencies: string[]string array of dependencies, these are unique names that you used to register the dependencies
isSingleton: booleandefault value is false.

get(name):an y

ParameterDescription
name: stringunique name of the dependency you need

Contribution

Become a contributor to this project. Feel free to submit an issue or a pull request.

1.1.0

2 years ago

1.0.9

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago