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 Build Status gzip size size

This library implements dependency injection for javascript. It is currently work in progress and not jet released.

Usage

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

Create a folder ioc and add the new file ioc/types.ts:

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

Next need a container to bind your dependencies to. Lets create the file ioc/container.ts

import {Container, inject} 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();

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

export {container, TYPE, inject};
Step 3 - Inject your dependency

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

import {container, TYPE, inject} from "./ioc/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 you run this example you should see the content of your example services.

Unit testing with IoC

You can snapshot and restore a container for unit testing. You can make multiple snapshots in a row, but that is not recommend.

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

I am working on the first release. Current state can you see in the Github Project.

Inspiration

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

  1. I want to make a very lightweight solution
  2. I want implement less features to be more straight forward
  3. Always lazy inject the dependencies

License

License under Creative Commons Attribution 4.0 International

Copyright © 2019 Hauke Broer

Keywords

FAQs

Last updated on 19 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