![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
IoC(Inversion of Control)控制反转,是面向对象编程中的一种设计原则,用来降低计算机代码之间的耦合度,而 DI 则是实现IOC的一种实现技术,简单来说就是我们将依赖注入给调用方,而不需要调用方来主动获取依赖。
npm install @tanbo/di reflect-metadata
要实现自动依赖注入,在 @tanbo/di 中,要用到 ReflectiveInjector
类作为容器来实现。同时还要把需要可注入的类标识为 Injectable
。
import 'reflect-metadata';
import { Injectable, ReflectiveInjector } from '@tanbo/di';
// 声明类是可注入的
@Injectable()
class Child {
name = 'child'
index: number
constructor() {}
}
// 声明类是可注入的
@Injectable()
class Parent {
name = 'parent'
constructor(public child: Child) {
}
}
// 创建容器
const injector = new ReflectiveInjector(null, [Parent, Child]);
// 获取实例
const instance = injector.get(Parent);
console.log(instance);
在实际应用中,很多时候不仅仅只需要注入类的实例,可能还需要注入其它数据,但通过 Typescript 自动解析元数据,是无法获取到相关依赖信息的。这时,就需要通过指定 token 的方式实现。
import { Injectable, InjectionToken, Inject } from '@tanbo/di';
interface UserInfo {
name: string;
}
const USER_INFO_INJECTIOON_TOKEN = new InjectionToken<UserInfo>('USER_INFO_INJECTIOON_TOKEN');
@Injectable()
class User {
constructor(@Inject(USER_INFO_INJECTIOON_TOKEN) public userInfo: UserInfo) {
}
}
const injector = new ReflectiveInjector(null, [
User, {
provide: USER_INFO_INJECTIOON_TOKEN,
useValue: {
name: '张三'
}
}
]);
const instance = injector.get(User);
FAQs
A dependency injection Library
The npm package @tanbo/di receives a total of 5 weekly downloads. As such, @tanbo/di popularity was classified as not popular.
We found that @tanbo/di demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.