Skip to content

A simple Typescript inversion of control (IOC) container

Notifications You must be signed in to change notification settings

ravinpatel/IOC-Container

Repository files navigation

IOC-Container

A simple inversion of control (IOC) container. TypeScript based lightweight annotation-based dependency injection container.

How to install

You can install this package using NPM:

npm i ioc-container-simple

How to use (examples)

Class injection

You can use inject decorator to inject dependencies into properties and methods of a class

import { Registry, registry, inject } from '@ravinpatel/ioc-container-simple';

class C {
  @inject() public readonly registry: Registry
}

class B {
  @inject() public readonly c: C
}

class A {
  @inject() public readonly b: B
}


@registry([A, B, C])

class App {
  @inject()
  public readonly a: A
}

Injecting Dependencies with Custom Names

Specify custom names for your dependencies

import { inject } from './inject';

class MyClass {
  @inject({ name: 'CustomNameForDependencyA' })
  dependencyA!: DependencyA;

  @inject({ name: 'CustomNameForDependencyB' })
  dependencyB!: DependencyB;
}

Injecting Dependencies with Optional Scopes

Specify optional scopes for your dependencies

import { inject } from './inject';

class MyClass {
  @inject({ name: 'CustomNameForDependencyA' })
  dependencyA!: DependencyA;

  @inject({ name: 'CustomNameForDependencyB' })
  dependencyB!: DependencyB;
}

Testing

To run the tests, use:

npm run test

Linting

To check ESLint rules, use:

npm run lint

About

A simple Typescript inversion of control (IOC) container

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published