1.0.28 • Published 1 year ago

@infini-dev/inject v1.0.28

Weekly downloads
-
License
Unlicense
Repository
github
Last release
1 year ago

Getting Started

Common design pattern to write loosely coupled code and being polite to others reading it.

Common use case
Reusability, scalability Avoid code duplication

Install

Run of one these command to install.

$ npm i @infini-dev/inject

or

$ yarn add @infini-dev/inject

Examples

To add, remove and consume a dependency.

Add

import { Container } from "@infini-dev/inject";

const inject = Container();
inject.add("dependency", (a)=>a*2)

Initialize

import { Container } from "@infini-dev/inject";

const inject = Container({
  test: (a)=>a*2
});

Remove

import { Container } from "@infini-dev/inject";

const inject = Container({
  test: (a)=>a*2
});

inject.remove("test")

Consume

import { Container } from "@infini-dev/inject";

const inject = Container({
  test: (a)=>a*2
});

const myTest = inject.getInstance("test")
console.log(myTest(2))

Learn

Let's create program to show weather. Everywhere in that app is required to have the weather.

We could write everywhere to fetch the weather directly.

What is going to happen if the weather url change? It's going to be required to walk trhough code and replace to another url. How are you going to test if url is different somewhere.

Using Dependency Injection, it's possible to replace the implementation without having impact somewhere else. In this case, changing url.

import { Container } from "@infini-dev/inject";
// Initialize
const inject = Container({
  getWeather: await fetchWeather("https://my.weather.api.com")
});


// Consumer where you need it
const weather = inject.getInstance("getWeather")
console.log(weather())

Powered 🚀 by Infinisoft Inc. Wanna code the future? Come with us https://www.infinisoft.dev