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

ioc-service-container

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ioc-service-container

Lightweight ioc service container

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
343
increased by174.4%
Maintainers
1
Weekly downloads
 
Created
Source

ioc-service-container

Code Smells Bugs

This is a lightweight library for a service container written in TypeScript.

Buy Me A Coffee

Get started

Install the dependency with npm install ioc-service-container

Usage

First set up an Enum for preventing typos or redefinition of service ids:

export enum ServiceId {
  TestApi = 'TestApi',
  TestService = 'TestService',
  FooApi = 'FooApi',
}

According to this you have to pass a factory of your required services to the ioc container. So at the initial script of your application you call a function named e.g. setupServiced:

function setupServiced() {
  ServiceContainer.set(ServiceId.TestApi, () => new CustomTestApi());
  ServiceContainer.set(ServiceId.FooApi, () => new CustomFooApi());
  ServiceContainer.set(ServiceId.TestService, () => new CustomTestService());
}

Now you have two options to inject the requested service. The first one is without the usage of TypeScript annotations. This can be used anywhere in your code:

import { ServiceId } from './ServiceId';
import { ServiceContainer } from 'ioc-service-container';

const testService = ServiceContainer.get<TestService>(ServiceId.TestApi);
const testApi = ServiceContainer.get<TestService>(ServiceId.TestService);

The second option is to use the @inject decorator inside a class:

export class CustomTestService implements TestService {
  @inject
  private readonly testApi!: TestApi; // Important is the naming of the property, its mapped to the sericeId

  @injectViaId(ServiceId.FooApi)
  private readonly nameThisHowYouWant!: FooApi // If you don't want to name your property like the service id, use this decorator
}

Your can see a demo in the ./example folder. To run this type in npm run example.

Background

Structuring your code and avoiding implizit dependencies is two of the most effective ways to avoiding bugs, especially when code gets extended. To goal of Dependency Injection (DI) is to prevent structures like this:

class CustomService {
  constructor() {
    this.api = new CustomApi();
  }
}

The CustomService has an implizit dependency to the CustomApi.

Goal

The goal of DI is to encapsulate the dependencies of a class. The CustomService should work without knowing which api it is using. Following structure should be created:

+----------+    +-------------------+
|          |    |                   |
| Consumer +--->+ interface Service |
|          |    |                   |
+----------+    +---------+---------+
                          ^
                          |
                          |
                +---------+-----------+     +----------------+
                |                     |     |                |
                | class CustomService +---->+  interface Api |
                | implements Service  |     |                |
                |                     |     +--------+-------+
                +---------------------+              ^
                                                     |
                                                     |
                                            +--------+--------+
                                            |                 |
                                            | class CustomApi |
                                            | implements Api  |
                                            |                 |
                                            +-----------------+

(Btw asciiflow.com is a great tool for creating small charts for e.g. Readme.md)

FAQs

Package last updated on 15 Feb 2021

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