
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
FAQs
A Dependency Injection library
The npm package @searchfe/inject-js receives a total of 202 weekly downloads. As such, @searchfe/inject-js popularity was classified as not popular.
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.