Socket
Socket
Sign inDemoInstall

@owja/ioc

Package Overview
Dependencies
0
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @owja/ioc

dependency injection for javascript


Version published
Weekly downloads
1.3K
decreased by-37.37%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

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 beta phase but the API should not change anymore before 1.0.0 stable release will arrived.

Usage

Step 1 - Installing the OWJA! IoC library
npm install --save-dev @owja/ioc
Step 2 - 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 3 - 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 4 - 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

Keywords

FAQs

Last updated on 23 May 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc