0
点赞
收藏
分享

微信扫一扫

Typescript + 反射实现Ioc DI依赖注入

三次方 2022-02-08 阅读 56

Typescript + 反射实现Ioc DI依赖注入

  • 参考资料:

深入Typescript:Reflect Metadata

https://github.com/rbuckton/reflect-metadata#api

  • 有关开启、安装反射和装饰器参考深入Typescript:Reflect Metadata

反射的基本使用

import 'reflect-metadata';

function Class(): ClassDecorator{
    return (target: any) => {}
}

function Method(): MethodDecorator{
    return (target: Object, key: string) => {}
}

function Prototype(): PropertyDecorator {
    return (target: any, key: string) => {}
}

@Class()
class Demo {
    @Prototype()
    private static readonly author: string = 'xiaoqinvar';

    @Prototype()
    private readonly date: Date = new Date();

    constructor(private readonly service: number) { }

    @Method()
    say(num: number): void {}

    @Method()
    static talk(str: string): Promise<void> {
        return null;
    }
}

反射的基本使用:自定义metadataKey参考深入Typescript

// 构造器参数列表:[ [Function: Number] ]
console.log(Reflect.getMetadata("design:paramtypes", Demo));

// 获取Demo类中say方法的类型: [Function: Function]
console.log(Reflect.getMetadata("design:type", new Demo(1), 'say')); 

// 获取Demo类中say实例方法的参数类型: [ [Function: Number] ]
console.log(Reflect.getMetadata("design:paramtypes", new Demo(1), 'say')); 

// 获取Demo类中talk静态方法的参数类型: [ [Function: String] ]
console.log(Reflect.getMetadata("design:paramtypes", Demo, 'talk')); 

// 获取Demo类中talk静态方法的返回值类型: [Function: Promise]
console.log(Reflect.getMetadata("design:returntype", Demo, 'talk')); 

// 获取静态属性的类型:[Function: String]
console.log(Reflect.getMetadata("design:type", Demo, 'author')); 

// 获取成员属性的类型:[Function: Date]
console.log(Reflect.getMetadata("design:type", new Demo(1), 'date'));

// 获取存在的key
// 实例成员用实例 [ 'design:type' ]
console.log(Reflect.getMetadataKeys(new Demo(1), 'date')); 

// 静态成员用类即可 [ 'design:returntype', 'design:paramtypes', 'design:type' ]
console.log(Reflect.getMetadataKeys(Demo, 'talk')); 
  • 注意:在获取的类型上一定要添加装饰器,比如:需要获取构造函数的参数类型,那就必须在类上添加注解才能获取

IOC DI依赖注入

  • 学习了基础反射后就可以实现依赖注入了
import 'reflect-metadata';

// 利用反射获取类型。再创建对象并调用run方法
function Autowried(): ClassDecorator {
  return (target: any) => {
    const ref = Reflect.getMetadata('design:paramtypes', target);
    ref.map(item => new item())
    new target(...ref).run();
  }
}

// Service类只有一个uname只读成员属性
class Service { readonly uname: string = 'xiaoqinvar' }

@Autowried()
class Controller {

  readonly author: string = 'xiaoqinvar';

  constructor(private readonly service: Service) {}

  run(): void {
    console.log(`hello ${this.service.uname} 今天学习nest了吗?`);
  }
}
  • 这些内容理解了都很简单,无非就两个思想
    1. 装饰器器进行方法增强,比如说:express每次都要写路由,每次都是那几行代码,利用装饰器进行增强来达到用一个@xx来替换app.xxxxxxxxx一大段代码片段,当然了,里面肯定包含很多复杂的处理、过滤、校验等等…
    2. 反射,有了反射就能够进行Ioc Di(控制反转、依赖注入),而不用每次都需要手动new对象
举报

相关推荐

0 条评论