minimal-di
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Minimal DI

npm version badge

About

A minimalistic Dependency Injection library. Ideal por small projects that just want that extra testability and decoupling. All dependencies are lazy singletons, so your dependencies will only be resolved when needed and only once.

Usage

First you need to create a MinimalDIContainer instance.

import ( MinimalDIContainer } from 'minimal-di';

const diContainer = new MinimalDIContainer();

Then you register your dependency and how the container should resolve it

class SimpleComponent {
  doTheThing() {
    ...
  }
}
diContainer.register(SimpleComponent, () => new SimpleComponent());

And when you need your SimpleComponent, just get it!

const simpleComponent = diContainer.get(SimpleComponent);
simpleComponent.doTheThing();

To resolve dependencies of a dependency, just use diContainer.get inside your dependency resolver

class ComplexComponent {
  constructor(simpleComponent) {
    this.simpleComponent = simpleComponent;
  }
}
diContainer.register(ComplexComponent, () => new ComplexComponent(diContainer.get(SimpleComponent)));

Package Sidebar

Install

npm i minimal-di

Weekly Downloads

11

Version

1.0.1

License

ISC

Unpacked Size

8.53 kB

Total Files

12

Last publish

Collaborators

  • gus_hill