npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-dependency-injector

v1.0.3

Published

A lightweight dependency injection container that allows you to register and resolve dependencies in your application. It supports both lazy and non-lazy dependencies, as well as resolving chain dependencies.

Downloads

25

Readme

TS Dependency Injector

The ts-dependency-injector package provides a lightweight dependency injection container that allows you to register and resolve dependencies in your application. It supports both lazy and non-lazy dependencies, as well as resolving chain dependencies.

Features

  • Dependency Registration: Register both lazy and non-lazy dependencies using the register method.
  • Singleton Support: Register and resolve non-lazy singletons using the register method.
  • Chain Dependency Resolution: Resolve dependencies that have chain dependencies by registering them in the correct order.
  • Lazy Loading: Register lazy dependencies using the register method with the lazy parameter set to true.
  • Generic Support: Use generics to specify the type of the resolved dependency when calling the resolve method.
  • Dependency Reset: Reset all registered dependencies using the reset method.

Installation

You can install the ts-dependency-injector package using a package manager like npm or yarn. Open your terminal and run the following command:

npm install ts-dependency-injector

or

yarn add ts-dependency-injector

Usage

To use the ts-dependency-injector package in your application, follow these steps:

1. Import the ServiceLocator class

Import the ServiceLocator class into your file:

import { ServiceLocator } from 'ts-dependency-injector';

2. Register Dependencies

To register a dependency, use the register method of the ServiceLocator instance. You can register both lazy and non-lazy dependencies.

ServiceLocator.getInstance().register('DependencyA', () => new DependencyA());

For lazy dependencies, set the lazy parameter to true:

ServiceLocator.getInstance().register('DependencyB', () => new DependencyB(), true);

3. Resolve Dependencies

To resolve a dependency, use the resolve method of the ServiceLocator instance. Specify the key or name of the dependency, and the method will return the resolved instance.

const dependencyA = ServiceLocator.getInstance().resolve<DependencyA>('DependencyA');

4. Resolve Chain Dependencies

The ts-dependency-injector package supports resolving chain dependencies. Register the dependencies in the correct order.

ServiceLocator.getInstance().register('DependencyA', () => new DependencyA());
ServiceLocator.getInstance().register(
  'DependencyB',
  () => new DependencyB(ServiceLocator.getInstance().resolve<DependencyA>('DependencyA')),
);

5. Reset Dependencies

Reset all registered dependencies using the reset method.

ServiceLocator.getInstance().reset();

Example

Here's an example showcasing the usage of ts-dependency-injector:

import { ServiceLocator } from 'ts-dependency-injector';

class DependencyA {
  public name = 'DependencyA';
}

class DependencyB {
  constructor(public dependencyA: DependencyA) {}
  public name = 'DependencyB';
}

const serviceLocator = ServiceLocator.getInstance();

serviceLocator.register('DependencyA', () => new DependencyA());
serviceLocator.register('DependencyB', () => new DependencyB(serviceLocator.resolve<DependencyA>('DependencyA')));

const dependencyB = serviceLocator.resolve<DependencyB>('DependencyB');
console.log(dependencyB.name); // Output: DependencyB
console.log(dependencyB.dependencyA.name); // Output: DependencyA