You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

container-ioc

Package Overview
Dependencies
Maintainers
1
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

container-ioc

Dependency Injection and IoC container

1.6.4
Source
npmnpm
Version published
Weekly downloads
87
521.43%
Maintainers
1
Weekly downloads
 
Created
Source

alt text

container-ioc

is Dependency Injection and IoC container for Typescript projects (ES6+ API is coming). It manages the dependencies between classes, so that applications stay easy to change and maintain as they grow.

npm version Build Status npm license

Features:

  • Well-known Angular4+ DI API.
  • No external dependencies.
  • Singleton and Non-Singleton configuration.
  • Hierarchical containers.
  • Pluggable metadata annotator.
  • Can be used both on front-end and back-end.
  • 100% test coverage.

Installation:

npm install --save container-ioc

Quick start

import { Container, Inject, Injectable } from 'container-ioc';

let container = new Container();

@Injectable()
class App {}

interface IService {}

@Injectable()
class Service implements IService {
    constructor(@Inject('IService') public service: IService) {}
}

let providers = [
    { token: App, useClass: App }, 
    { token: 'IService', useClass: Service }
];

container.register(providers);
let app = container.resolve(App);

Best Practise: use InjectionToken instances for tokens instead of string/class literals:

Without InjectionToken:
interface IService {}

@Injectable()
class ConcreteService {}

container.register({ token: 'IService', useClass: ConcreteService });
container.resolve('IService');

With InjectionToken
interface IService {}

const TService = new InjectionToken<IService>('IService'); // T stands for Token, you can pick another prefix

@Injectable()
class ConcreteService {}

container.register({ token: TService, useClass: ConcreteService });
container.resolve(TService);

Examples:

Persistence control.

By default, resolved instances are singletons. You can change that by setting provider's attribute LifeTime to LifeTime.PerRequest.

import { Container, Injectable, LifeTime } from 'container-ioc';

const container = new Container();

@Injectable()
class A {}

container.register({ token: A, useClass: A, lifeTime: LifeTime.PerRequest });

const instance1 = container.resolve(A);
const instance2 = container.resolve(A);

// instance1 !== instance2

Hierarchical containers.

if a provider wasn't found in a container it will look up in ascendant containers if there are any:

import { Container } from 'container-ioc';

@Injectable()
class A {}

let parentContainer = new Container();
let childContainer = parentContainer.createScope();

parentContainer.register({ token: 'IA', useClass: A });

childContainer.resolve('IA');

Pluggable metadata annotator.

By default metadata is assigned to static properties. If you want to use Reflect API for annotation, you can implement IMetadataAnnotator interface with your implementation using Reflect API. Then plug it into AnnotatorProvider

import { AnnotatorProvider, IMetadataAnnotator, Container } from 'container-ioc';

class ReflectMetadataAnnotator implements IMetadataAnnotator {
    // your implementation
}

AnnotatorProvider.set(new ReflectMetadataAnnotator());

let container = new Container();

...

Contribution:

Feel free to submit a bug or a feature request. Or pick an issue from here and leave a comment with your proposal.

see CONTRIBUTION.md

Keywords

ioc

FAQs

Package last updated on 04 Oct 2017

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