owja/ioc


unicorn lightweight (<1kb) inversion of control javascript library for dependency injection written in typescript

License: CC-BY-4.0

Language: TypeScript

Keywords: dependency-injection, inversion-of-control, ioc, ioc-container, javascript, typescript


OWJA! IoC

npm version codecov Greenkeeper badge Build Status gzip size size

This library implements dependency injection for javascript. It is currently work in progress and in unstable alpha phase. The API can change until 1.0.0 stable release.

Usage

Step 1 - Installing the OWJA! IoC library
npm install --save-dev @owja/ioc
Step 3 - Create symbols for our dependencies

Now we create the folder services and add the new file services/types.ts:

export const TYPE = {
    "MyService" = Symbol.for("MyService"),
    "MyOtherService" = Symbol.for("MyOtherService"),
};
Step 2 - Creating a container

Next we need a container to bind our dependencies to. Let's create the file services/container.ts

import {Container, createDecorator} from "@owja/ioc";

import {TYPE} from "./types";

import {IMyService, MyService} from "./service/my-service";
import {IMyOtherService, MyOtherService} from "./service/my-other-service";

const container = new Container();
const inject = createDecorator(container);

container.bind<IMyService>(TYPE.MyService).to(MyService);
container.bind<IMyOtherService>(TYPE.MyOtherService).to(MyOtherService);

export {container, TYPE, inject};
Step 3 - Injecting dependencies

Lets create a example.ts file in our source root:

import {container, TYPE, inject} from "./services/container";
import {IMyService} from "./service/my-service";

class Example {
    @inject(TYPE.MyService)
    readonly myService!: IMyService;
    
    @inject(TYPE.MyOtherSerice)
    readonly myOtherService!: IMyOtherService;
}

const example = new Example();

console.log(example.myService);
console.log(example.myOtherSerice);

If we run this example we should see the content of our example services.

Unit testing with IoC

We can snapshot and restore a container for unit testing. We are able to make multiple snapshots in a row too.

beforeEach(() => {
    container.snapshot();
});

afterEach(() => {
    container.restore();
}

test("can do something", () => {
    container.rebind<MyServiceMock>(TYPE.MySerice).to(MyServiceMock);
    const mock = container.get<MyServiceMock>(TYPE.MySerice);
});

Development

We are working on the first stable release. Current state of development can be seen in our Github Project for the first release.

Inspiration

This library is highly inspired by InversifyJS but has other goals:

  1. Make the library very lightweight (less than one kilobyte)
  2. Implementing less features to make the API more straight forward
  3. Always lazy inject the dependencies
  4. No meta-reflect required

License

License under Creative Commons Attribution 4.0 International

Copyright © 2019 Hauke Broer

Project Statistics

Sourcerank 2
Repository Size 274 KB
Stars 1
Forks 0
Watchers 1
Open issues 3
Dependencies 755
Contributors 1
Tags 2
Created
Last updated
Last pushed

Top Contributors See all

Hauke B.

Packages Referencing this Repo

@owja/ioc
dependency injection for javascript
Latest release 1.0.0-alpha.2 - Updated - 1 stars

Recent Tags See all

v1.0.0-alpha.2 May 21, 2019
v1.0.0-alpha.0 May 18, 2019

Something wrong with this page? Make a suggestion

Last synced: 2019-05-22 01:09:33 UTC

Login to resync this repository