@fluffy-spoon/inverse
TypeScript icon, indicating that this package has built-in type declarations

1.35.0 • Public • Published

@fluffy-spoon/inverse is an Inversion of Control framework.

Installing

npm i @fluffy-spoon/inverse --save

TypeScript requirements

This framework requires the following values set in your TypeScript configuration (tsconfig.json):

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "esModuleInterop": true
    }
}

Examples

These examples assume that there's a Bar class which as some dependencies (A1 and A2), which then further have some dependencies (B1 and B2) as shown below.

Note how every class has an Injectable decorator, and how every dependency has an Inject decorator. That's all that is needed! The examples below would work for exported classes from separate files too.

@Injectable
class Bar {
    constructor(
        @Inject private a: A1, 
        @Inject private b: A2, 
        @Inject private c: A2) 
    {
    }
}

@Injectable
class B1 {

}

@Injectable
class B2 implements B1 {

}

@Injectable
class A1 {
    constructor(
        @Inject private a: B1) 
    {
    }
}

@Injectable
class A2 {
    constructor(
        @Inject private a: B2) 
    {
    }
}

Basic use

In the following example, a Bar instance is requested from the IOC container. Per default, each dependency will be instantiated recursively with its own constructor.

The following code:

import Container from '@fluffy-spoon/inverse';

const container = new Container();

const bar = container.resolve(Bar);

Is equal to:

const bar = new Bar(
    new A1(new B1()),
    new A2(new B2()),
    new A2(new B2()));

Changing dependencies

In the below example, we change all B1 to be instances of B2 instead. This is possible because B2 implements B1.

The following code:

import Container from '@fluffy-spoon/inverse';

const container = new Container();
container.whenRequestingType(B1).useType(B2);

const bar = container.resolve(Bar);

Is equal to:

const bar = new Bar(
    new A1(new B2()),
    new A2(new B2()),
    new A2(new B2()));

You can also use:

  • useFactory for determining how an instance should be created.
  • useRequestedType to use the requested type (this is default).

Changing lifetime

In the below example, we make B2 instances be single-instance by using the asSingleInstance method.

Note that useRequestedType is called here. This just means that when a B2 instance is requested, a B2 instance is also injected (which is default).

import { Container } from '@fluffy-spoon/inverse';

const container = new Container();
container.whenRequestingType(B2).useRequestedType().asSingleInstance();

const bar = container.resolve(Bar);

Is equal to:

const b2 = new B2();
const bar = new Bar(
    new A1(new B1()),
    new A2(b2,
    new A2(b2)));

You can also use:

  • asInstancePerRequest to create a new instance from the type or factory provided every time (this is default).

Framework support

@fluffy-spoon/inverse can be used without a framework, but more packages can be used if you want to tightly integrate it.

Readme

Keywords

none

Package Sidebar

Install

npm i @fluffy-spoon/inverse

Weekly Downloads

0

Version

1.35.0

License

ISC

Unpacked Size

84.4 kB

Total Files

35

Last publish

Collaborators

  • ffmathy