This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

@infini-dev/inject
TypeScript icon, indicating that this package has built-in type declarations

1.0.28 • Public • Published

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 @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]

Readme

Keywords

none

Package Sidebar

Install

npm i @infini-dev/inject

Weekly Downloads

0

Version

1.0.28

License

Unlicense

Unpacked Size

12.1 kB

Total Files

20

Last publish

Collaborators

  • npm-support