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 🙏

© 2026 – Pkg Stats / Ryan Hefner

light-di

v1.3.0

Published

LightDI - TypeScript Dependency Injection Framework - 服务之间只通过接口依赖,实现解耦

Readme

LightDI - TypeScript 依赖注入框架

English | 中文

安装

npm install light-di

设计理念

LightDI 旨在实现服务之间的解耦:服务之间只通过接口依赖,无需关注具体实现。通过依赖注入,可以轻松替换实现类,提高代码的可维护性和可测试性。

核心功能

1. 绑定和获取服务

  • bind<IxxxService>(ImplClass, key?) - 绑定服务实现类
  • getService(InterfaceClass, key?) - 获取服务实例

key 的作用:用于实现一个接口有多个实例的场景。同一个接口可以通过不同的 key 绑定多个实例,默认 key 为 default

2. ManagerProvider 自动创建

如果 getService 找不到实例,会自动调用实现类的 ManagerProvider() 方法创建实例并缓存,无需手动 new 实现类

使用要求

接口文件 (IxxxService.ts)

  • 继承 ILightDIBaseService
  • 只包含接口定义

实现文件 (xxxImpl.ts)

  • 实现接口
  • 使用 @ServiceImpl(InterfaceClass) 装饰器
  • ManagerProvider() 方法由装饰器自动创建

示例

定义服务

接口定义 (IBizA.ts)

import { ILightDIBaseService } from 'light-di';

export abstract class IBizA extends ILightDIBaseService {
  abstract value: number;
  abstract print(): void;
}

实现定义 (BizAImpl.ts)

import { IBizA } from './IBizA';
import { ServiceImpl } from 'light-di';

@ServiceImpl(IBizA)
export class BizAImpl implements IBizA {
  public value: number = 10;
  public print(): void {
    console.log('BizA:', this.value);
  }
}

基本使用

import { bind, getService } from 'light-di';
import { IBizA } from './IBizA';

// 方式1: 手动绑定实现类
bind<IBizA>(BizAImpl);

// 方式2: 自动创建(无需手动 new)
// 直接获取服务,如果未绑定会自动调用 ManagerProvider() 创建实例
const bizA = getService(IBizA); // 自动创建 BizAImpl 实例

// 多实例绑定(使用不同的 key)
bind<IBizA>(BizAImpl, 'key1');
const bizAWithKey1 = getService(IBizA, 'key1'); // 获取 key1 对应的实例
const bizADefault = getService(IBizA); // 获取 default key 对应的实例

服务依赖示例

import { ServiceImpl, getService } from 'light-di';
import { IBizA } from './IBizA';
import { IBizB } from './IBizB';

// BizB 依赖 IBizA,但只需要依赖接口,无需知道具体实现
@ServiceImpl(IBizB)
export class BizBImpl implements IBizB {
  private bizA: IBizA;

  constructor() {
    // 通过接口获取依赖,实现解耦
    this.bizA = getService(IBizA);
  }
}

详细示例见 index.tsexample/ 目录。