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

di-simple

v1.0.0

Published

简易版本的依赖注入系统

Readme

DI-simple

简易版的 依赖注入

DI(依赖注入)的发展

  • 设计模式

    刚开始,我们更习惯将其称之为一种设计模式,一种职责转移,一个类对外部类的依赖只需要声明在构造函数中即可,具体的依赖类的实现细节以及创建方式,不需要关心,交给这个类的使用者来负责。这种方式好像是:类对想创建它的消费者说,hey,想创建我,当然可以,但是,我还依赖 xxx, 你需要先创建它们,然后再来把它们给我。

    这种方式,很明显,有一定的优点,但是缺点同样不容小视,每次创建类,最为消费者,我都需要先创建它的所有依赖,假如要创建1000次呢?

  • 框架级别

    慢慢发展,我们发现,可以将这种依赖统一管理,对使用者透明。由框架统一管理对象的创建,消费者只需要声明它们的依赖即可,在需要的时候,直接创建就可以了.

使用方式

import { Inject, Injector } from './src/index';

class A{
    title:string = '123';
}

class B{}

class C{}

class D{
    title:string = 'abc';
    constructor(@Inject('A') a, @Inject('B') b){
        this.a = a;
        this.b = b;
    }

    test() {
        console.log('assert equel 123', this.a.title);
    }
}



// 创建一个注入器
const injector = Injector.resolveAndCreate([A, B, C, D]);


const a1 = injector.get('A');
const a2 = injector.get('A');

// 两者应该是同一个对象
console.log(`output: ${ a1 == a2 }`);

更多使用,参考测试用例以及demo目录下的示例