redi
redi (发音为 'ready')是 TypeScript 的依赖注入库,另外提供了一组 React 绑定。借助依赖注入模式,写出更少耦合、更易调整、更易维护的代码。
💡
不了解依赖注入,或者没想好要不要使用依赖注入?请请阅读我们的博客以了解依赖注入想要解决的问题以及其优点。
概览
import { Inject } from '@wendellhu/redi'class AuthService { public getCurrentUserInfo(): UserInfo { // your implementation here... }}class FileListService { constructor(@Inject(AuthService) private readonly authService: AuthService) {} public getUserFiles(): Promise<Files> { const currentUser = this.authService.getCurrentUserInfo() // ... }}const injector = new Injector([[AuthService], [FileListService]])injector.get(AuthService)
立即开始 | Demo TodoMVC | Demo 仓库 | 脚手架
功能
- 非侵入,redi 并不强制你在整个项目使用依赖注入模式,你可以在任何想要的地方应用
- 支持多种类型的依赖项,包括:类、JavaScript 原始类型值和对象、工厂函数、异步依赖项
- 支持可选、多值依赖注入
- 支持层次化依赖注入,支持对依赖查找过程进行控制
- 支持惰性实例化