2.1.1 • Published 1 year ago

@microkits/microinjection v2.1.1

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

Microinjection

Image of Microinjection

Connecting everything is a tedious part of app development. Microinjection aims to make development easier and faster by helping you write testable, loosely coupled, and maintainable applications.

Microinjection is written entirely in TypeScript, but supports JavaScript as well.

npm version CodeQL

Features

  • Property Injection
  • Constructor Injection
  • Multiple DI containers
  • Registering and resolving dependencies hierarchically
  • Singleton, Transient, and Context scopes
  • Circular dependencies
  • Out-of-the-box support for injecting values, factories and classes.

Installation

Microinjection is available as a package on NPM.

To install and save in your package.json dependencies, run the following command:

Install with npm:

npm install @microkits/microinjection

Install with yarn:

yarn add @microkits/microinjection

Basic usage

Containers are the main components of Microinjection. The first step to start using Microinjection is to getting a container.

import { Microinjection } from "@microkits/microinjection";

const container = Microinjection.getDefaultContainer();

With an instance of the Container in hand, you can add your Registrations.

class Cat {
  speak() {
    console.log("meooow!")
  }
}

class CatOwner {
  cat: Cat;
}

container.register("Cat").asClass(Cat);
container.register("CatOwner").asClass(CatOwner, {
  properties: [{
    name: "cat",
    inject: "Cat"
  }]
});

Now, you can request that the Container resolve a Registration for you. It will also resolve all necessary dependencies.

// Container will inject an instance of Cat on resolved CatOwner.
const owner = container.resolve<CatOwner>("CatOwner");

owner.cat.speak();
// logs "meooow!" to the console
2.1.1

1 year ago

2.1.0

1 year ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.1-beta

2 years ago

1.0.0-beta

2 years ago

0.0.1

2 years ago