1.7.3 • Published 4 years ago

@here-mobility/micro-di v1.7.3

Weekly downloads
34
License
MIT
Repository
github
Last release
4 years ago

micro-di

A lightweight, minimal module for allowing dependency-injection in your frontend app. Written in TypeScript and recommended to use mostly in TypeScript projects that care about artifact size.

License Build Status Coverage Status

What's in it for me?

Dependency Injection (DI) is a design pattern used to implement IoC (Inversion of Control). It allows the creation of dependent objects outside of a class and provides those objects to a class through different ways. Using DI, we move the creation and binding of the dependent objects outside of the class that depends on them.

The Dependency Injection pattern usually involve following types of classes:

Client Class: The client class (dependent class) is a class which depends on the service class.

Service Class: The service class (dependency) is a class that provides service to the client class.

Container Class: The container class is a class that associates token with the service class.

Injector Class: The injector class injects the token into the client class following by its internal logic.

Later the client will resolve the injected token from the container and receive an instance of the service or another compatible class. The following figure illustrates the relationship between these classes:

alt text

As you can see, in this approach, the DI pattern separates the responsibility of creating an object instance from the service class outside of the dependent client class, which reduces coupling between service and client classes.

Features

micro-di is simple to use. It implements only two actions required for DI - registration and resolution of the dependencies. When used with decorators, micro-di allows building the dependency graph "automatically" inline with the class definition. Dependencies are registered in the IoC container as functions that return an object instance (ctor) and are lazily loaded (resolved only upon first access).

Limitations

This library does not support cyclic dependency detection, it is up to the developer to avoid a dependency graph with cycles. Also, micro-di has only one global IoC container. Each class can have only one entry per class token. Named string tokens are unlimited but have to be unique.

Installation

To install using the npm package manager, run the following command:

$ npm install @here-mobility/micro-di --save

To install using the yarn package manager:

$ yarn add @here-mobility/micro-di

Project Configuration

Following parameters should be enabled in your tsconfig.json:

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

Usage

Dependency Registration

Register SomeFactory as a dependency using RegisterDependency method. This can be done anywhere in your application code:

import { RegisterDependency } from "@here-mobility/micro-di";
import { SomeFactory } from "some-factory.js";

RegisterDependency(SomeFactory, () => new SomeFactory("param"))

Register SomeClass using RegisterSingleton method. RegisterSingleton registers a resolver function which will be resolved to the constructed instance the first time SomeClass dependency is accessed. After the first time, the resolved instance will be stored in the container and subsequent access to SomeClass will always return the same instance as a Singleton.

import { RegisterSingleton } from "@here-mobility/micro-di";
import { SomeClass } from "some-class.js";

RegisterSingleton(SomeClass, () => new SomeClass("param"))

It is possible to register dependencies by using Experimental-Decorators. The same two options are available with decorators. Resolvable decorator registers resolver function as is:

import { Resolvable } from "@here-mobility/micro-di";

const counter = 0;

@Resolvable(() => new NameFactory())
@Resolvable("NameFactory") // Register multiple distinct dependencies
class NameFactory {
  getName() {
    return `Token#${counter}`
  }
}

Singleton decorator will register a single instance of this object:

import { Singleton } from "@here-mobility/micro-di";

@Singleton()
class Locator {
  constructor(name) {
    ...
  }
}

Dependency Resolution

Dependency can be resolved by using Resolve method. This can be done anywhere in your application code:

import { Resolve } from "@here-mobility/micro-di";
import { NameFactory } from "named-factory.js";

const instance = Resolve(NameFactory);
const second = Resolve<NameFactory>("NameFactory"); // String token can be resolved to any type

Dependency can be injected to constructor arguments automatically:

import { MapInject, Construct } from "@here-mobility/micro-di";

const instance = Construct(DependantClass);

class DependantClass {
  name: string = "noname";

  constructor(
    @Inject("NameFactory")
    private factory: NameFactory,

    @Inject(Locator)
    private prop: Locator
  )

  giveName() {
    this.name = this.factory.getName();
  }
}

Dependency can also be injected by using experimental-decorator Dynamic or Lazy:

import { Dynamic, Lazy } from "@here-mobility/micro-di";
import { Locator } from "locator.js";

class DependantClass {
  @Dynamic("NameFactory")
  factory!: NameFactory;

  @Lazy(Locator)
  private prop!: Locator;

  name: string = "noname";

  giveName() {
    this.name = this.factory.getName();
  }
}

Overriding Dependencies in tests

Dependencies in unit tests can be simply overridden with relevant mocks without touching the properties of the actual object under test. Use OverrideResolver and OverrideSingleton methods where applicable accordingly:

Example of register-mocks.js:

import { MockFactory, MockLocator } from "mocks"
import { Locator } from "locator.js";
import { NameFactory } from "named-factory.js";

OverrideSingleton(Locator, () => new MockLocator());
OverrideResolver(NameFactory, () => new MockFactory());

Using in the test case:

import "./register-mocks.js";
import { DependantClass } from "@/dependant-class.ts";
import { MockFactory } from "mocks"

describe("DependantClass", () => {
  const testedClass = DependantClass();
  const mockFactory = Resolve("NameFactory") as MockFactory;

  it("can be named", () => {
    testedClass.giveName();
    expect(mockFactory.lastGetNameCaller).toEqual(testedClass);
    expect(testedClass.name).toEqual(mockFactory.mockedName);
  });
});
1.7.3

4 years ago

1.7.2

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.6.0

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.4

4 years ago

1.4.3

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.0

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.11

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.3

4 years ago

1.0.0

4 years ago