
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
@searchfe/inject-js
Advanced tools
a dependency Injection library.
依赖注入lib库
使用 npm
来安装依赖
npm install --save @searchfe/inject-js
修改你的 tsconfig.json
包含以下的设置
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
另外因为该库依赖Reflect的使用,需要安装Reflect API的polyfill,包括但不限于:
类装饰injectable会在运行时将该class的依赖自动注入,内部通过获取该class的metadata来实现。
// LogService.ts
import { injectable } from 'inject-js';
import { QueryInfo } from './queryInfo.service';
@injectable
class LogService {
constructor(queryInfo: QueryInfo) {}
}
// index.ts 调用入口文件
import { Container } from 'inject-js';
import { QueryInfo } from './queryInfo.service';
import { LogService } from './LogService.ts;
const di = new Container();
di.addService(QueryInfo);
di.addService(LogService);
di.create(LogService);
参数装饰器inject会在运行时找到inject的token值进行注入。
import { inject } from 'inject-js';
import { QueryInfo } from './queryInfo.service';
@injectable
class LogService {
constructor(
@inject('QueryInfo') private queryinfo: QueryInfo
) {}
}
// index.ts 调用入口文件
import { Container } from 'inject-js';
import { QueryInfo } from './queryInfo.service';
import { LogService } from './LogService.ts;
const di = new Container();
di.LogService('QueryInfo', QueryInfo);
di.addService(LogService);
di.create(LogService);
也可以直接使用service去声明一个容器的service
@service(container)
class FooService {}
// 相当于:
class FooService {}
container.addService(FooService)
依赖注入容器
根据控制反转的概念: Inversion of Control (IoC),Container作为控制反转的容器,当你给容器提供一个toke时,容器会自动的根据这个token值去注入对应的依赖,而这个功能的实现是依赖上述的 inject
以及 injectable
去实现的。
一个注入的token可以是 class construct, Symbol, 或者 string
type InjectToken = Service | Symbol | string
在container中,有一个私有变量providers,用于提供给容器去根据其token加载对应的实例。在我们的lib库里面,提供了以下3中类型的provider:Service Provider/Value Provider/Factory provider,并且可以通过对应的方法注册不同的provider。
container的addProvider方法可以为该容器注册一个provider,每个provider需要提供create初始化方法,返回该provider注入的依赖。
service类型的provider可以调用addService来进行注册,inject-js内部的service-provider-impl会将传入的class注册到容器上,初始化时会生成一个实例注入到容器中。
value类型的provider可以调用addValue来进行注册,inject-js内部的value-provider-impl会将传入的value值注册到容器上,初始化时会自动注入到容器中。
factory类型的provider可以调用addFactory来进行注册,inject-js内部的factory-provider-impl会将传入的工厂方法注册到容器上,初始化时传入工厂的参数完成初始化注入到容器中。
FAQs
A Dependency Injection library
We found that @searchfe/inject-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 16 open source maintainers 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
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.