@brainstack/microinject
Banner
160 bytes
0 dependencies
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 @brainstack/microinject
or
$ yarn add @brainstack/microinject
Examples
To add, remove and consume a dependency.
Add
import { Container } from "@brainstack/microinject";
const inject = Container();
inject.add("dependency", (a)=>a*2)
Initialize
import { Container } from "@brainstack/microinject";
const inject = Container({
test: (a)=>a*2
});
Remove
import { Container } from "@brainstack/microinject";
const inject = Container({
test: (a)=>a*2
});
inject.remove("test")
Consume
import { Container } from "@brainstack/microinject";
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 "@brainstack/microinject";
const inject = Container({
getWeather: await fetchWeather("https://my.weather.api.com")
});
const weather = inject.getInstance("getWeather")
console.log(weather())
Powered 🚀 by Infinisoft World Inc.
Wanna code the future? Come with us [https://www.infinisoft.dev]