返回排行榜

Simple yet powerful dependency injection tool for JavaScript and TypeScript.

dependency-injectiontypescriptinversion-of-controlioctypedi
Star 增长趋势
Star
4.2k
Forks
165
周增长
Issues
44
2k4k
2015年9月2019年4月2022年12月2026年7月
制品库npmnpm install typedi
README

TypeDI

Build Status codecov npm version Dependency Status

TypeDI is a dependency injection tool for TypeScript and JavaScript. With it you can build well-structured and easily testable applications in Node or in the browser.

Main features includes:

  • property based injection
  • constructor based injection
  • singleton and transient services
  • support for multiple DI containers

Installation

Note: This installation guide is for usage with TypeScript, if you wish to use TypeDI without Typescript please read the documentation about how get started.

To start using TypeDI install the required packages via NPM:

npm install typedi reflect-metadata

Import the reflect-metadata package at the first line of your application:

import 'reflect-metadata';

// Your other imports and initialization code
// comes here after you imported the reflect-metadata package!

As a last step, you need to enable emitting decorator metadata in your Typescript config. Add these two lines to your tsconfig.json file under the compilerOptions key:

"emitDecoratorMetadata": true,
"experimentalDecorators": true,

Now you are ready to use TypeDI with Typescript!

Basic Usage

import { Container, Service } from 'typedi';

@Service()
class ExampleInjectedService {
  printMessage() {
    console.log('I am alive!');
  }
}

@Service()
class ExampleService {
  constructor(
    // because we annotated ExampleInjectedService with the @Service()
    // decorator TypeDI will automatically inject an instance of
    // ExampleInjectedService here when the ExampleService class is requested
    // from TypeDI.
    public injectedService: ExampleInjectedService
  ) {}
}

const serviceInstance = Container.get(ExampleService);
// we request an instance of ExampleService from TypeDI

serviceInstance.injectedService.printMessage();
// logs "I am alive!" to the console

Documentation

The detailed usage guide and API documentation for the project can be found:

Contributing

Please read our contributing guidelines to get started.

相关仓库
kataras/iris

The fastest HTTP/2 Go Web Framework. New, modern and easy to learn. Fast development with Code you control. Unbeatable cost-performance ratio :rocket:

GoGo ModulesBSD 3-Clause "New" or "Revised" Licensegoiris
iris-go.com
25.6k2.4k
Effect-TS/effect

Build production-ready applications in TypeScript

TypeScriptnpmMIT Licenseeffectjavascript
effect.website
15k620
alibaba/ARouter

💪 A framework for assisting in the renovation of Android componentization (帮助 Android App 进行组件化改造的路由框架)

JavaMavenApache License 2.0androidrouter
14.5k2.6k
google/wire

Compile-time Dependency Injection for Go

GoGo ModulesApache License 2.0gogolang
14.4k745
inversify/InversifyJS

Powerful and lightweight inversion of control container. Moved to https://github.com/inversify/monorepo

TypeScriptnpmMIT Licensetypescriptioc
inversify.io
12.1k711
jonataslaw/getx

Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.

DartMIT Licensegetxdependency-injection
11.2k1.9k
InsertKoinIO/koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform

KotlinApache License 2.0kotlindependency-injection
insert-koin.io
10k786
evrone/go-clean-template

Clean Architecture template for Golang services

GoGo ModulesMIT Licenseclean-architecturego
7.6k658
uber-go/fx

A dependency injection based application framework for Go.

GoGo ModulesMIT Licensegolanggo
uber-go.github.io/fx/
7.6k346
hyperf/hyperf

🚀 A coroutine framework that focuses on hyperspeed and flexibility. Building microservice or middleware with ease.

PHPPackagistMIT Licenseswoolecoroutine
hyperf.io
6.9k1.3k
Swinject/Swinject

Dependency injection framework for Swift with iOS/macOS/Linux

SwiftMIT Licensedependency-injectiondi-container
6.7k543
microsoft/tsyringe

Lightweight dependency injection container for JavaScript/TypeScript

TypeScriptnpmMIT Licensedependency-injectiondependency
6k182