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

tsnode-di

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsnode-di

Simple and lightweight dependency injection for Node TypeScript.

  • 0.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
86
decreased by-41.1%
Maintainers
1
Weekly downloads
 
Created
Source

TSNODE-DI

Build Status

Simple and lightweight dependency injection for Node TypeScript.

Getting Started

Installation

npm install --save tsnode-di

Usage

import { Injector } from 'tsnode-di';
import { Injectable } from 'tsnode-di';

@Injectable()
class ProviderClass {
	private aId: number;
	constructor() {
		this.aId = Math.ceil(Math.random() * 100000);
	}
	public get id(): number {
		return this.aId;
	}
}
@Injectable()
class ConsumerClass1 {
    // using constructor DI to resolve Provider class
    constructor(public provider: ProviderClass) { }
    public get provider():ProviderClass {
        return this.provider;
    }
}
@Injectable()
class ConsumerClass2 {
    // using @Resolve decorator to resolve ProviderClass
    @Resolve(ProviderClass)
    public provider: ProviderClass;
    constructor() { }
    public get provider():ProviderClass {
        return this.provider;
    }
}


// use Injector to resolve the consumer classes

const obj1 = Injector.resolve<ConsumerClass1>(ConsumerClass1);
const obj2 = Injector.resolve<ConsumerClass2>(ConsumerClass2);

// the following statement will not throw, because
// DI will provide the same instance of ProviderClass
// to both ConsumerClass1 and ConsumerClass2!
if(obj1.provider.id !== obj2.provider.id)
    throw new Error("DI Failed!");

FAQs

Package last updated on 10 Apr 2018

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