原文连接git
InversityJS 是一个 IoC 框架。IoC(Inversion of Control) 包括依赖注入(Dependency Injection) 和依赖查询(Dependency Lookup)。github
相比于类继承的方式,控制反转解耦了父类和子类的联系。api
import 'reflect-metadata' import { inject, injectable, Container } from 'inversify' const container = new Container() @injectable() class PopMusic { getName() { return '流行音乐' } } container.bind('request1').to(PopMusic) @injectable() class ClassicalMusic { getName() { return '古典音乐' } } container.bind('request2').to(ClassicalMusic) @injectable() class Music { pm: any cm: any constructor( @inject('request1') popMusic: any, @inject('request2') classicalMusic: any) { this.pm = popMusic this.cm = classicalMusic } getName() { const result = this.pm.getName() + this.cm.getName() return result } } container.bind('Plan').to(Music) const music: any = container.get('Plan') console.log(music.getName()) // 流行音乐古典音乐
上述案例能够抽象为下图:数据结构
虚线表示能够注入,但在代码中没有表现出来。框架
代码流程可归纳以下:函数
1.将全部相关类(这里指 Music、popMusic、classicMusic) 经过 @injectable
声明进 container
容器;this
2.经过 container.get()
获取 container.bind().to(target)
中的目标对象(这里指 Music);code
3.若是目标对象中的 constructor() 里有 @inject()
, 则将相应的实例(这里指 PopMusic 与 classicalMusic 的实例)看成构造函数的参数'注入';对象
inject 源码简化以下:blog
// 这是一个属性装饰器 function inject(serviceIdentifier) { return function (target, targetKey) { const metadataValue = { [targetKey]: [Metadata { key: 'inject', value: serviceIdentifier })] } Reflect.defineMetadata('inversify:tagged_props', metadataValue, target.constructor); } }
injectable 源码简化以下:
// 这是一个类装饰器 function injectable() { return function (target) { const metadataValue = [] Reflect.defineMetadata('inversify:paramtypes', metadataValue, target) return target } }
从简化版源码中能够看到 inject/injectable 最终是对 Reflect.defineMetadata()
的一个使用。能够将 metadata 当作是一种相对高效的数据结构。
InversityJS 深度结合了 reflect-metadata, reflect-metadata 在 Reflect 基础上对其 api 进行了扩展。
metadata 本质上是一个
WeakMap
对象。扩展:Map 和 WeakMap 的区别
Reflect.defineMetadata(metadataKey, metadataValue, target[, propertyKey])
简化版实现以下:
const Metadata = new WeakMap() function defineMetadata(metadataKey, metadataValue, target, propertyKey) { metadataMap = new Map() metadataMap.set(metadataKey, metadataValue) targetMetadata = new Map() targetMetadata.set(propertyKey, metadataMap) Metadata.set(target, targetMetadata) }
Reflect.getOwnMetadata(metadataKey, target[, propertyKey])
简化版实现以下:
function getOwnMetadata(metadataKey, target, propertyKey) { var targetMetadata = Metadata.get(target) var metadataMap = targetMetadata.get(propertyKey) return metadataMap.get(metadataKey) }
其数据结构可表示以下:
WeakMap { target: Map { propertyKey: Map { metadataKey: metadataValue } } }