Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@owja/ioc

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@owja/ioc

dependency injection for javascript

  • 1.0.0-alpha.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2K
decreased by-16.05%
Maintainers
1
Weekly downloads
 
Created
Source

OWJA! IoC

Build Status codecov

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

Usage

No package delivered jet!

Step 1 - Install the OWJA! IoC library
npm install --save-dev <unreleased>
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 <unreleased>;

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 snapshot and restore the registry", () => {
    container.rebind<MyServiceMock>(TYPE.MySerice).to(MyServiceMock);
    // ...
});

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

Package last updated on 18 May 2019

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc