微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

typedi 强大的javascript以及typescript 依赖注入框架

typedi 是typestack团队提供的依赖注入解决方案,对于typescript 我们可以使用注解的开发方式,官方的文档也比较详细

javascript 使用

var Service = require("typedi").Service;
var Container = require("typedi").Container;
var PostRepository = Service(() => ({
    getName() {
        return "hello from post repository";
    }
}));
var PostManager = Service(() => ({
    getId() {
        return "some post id";
    }
}));
class PostQueryBuilder {
    build() {
        return "SUPER * QUERY";
    }
}
var PostController = Service([
    PostManager,
    PostRepository,
    PostQueryBuilder
], (manager, repository, queryBuilder) => {
    return {
        id: manager.getId(),
        name: repository.getName(),
        query: queryBuilder.build()
    };
});
var postController = Container.get(PostController);
console.log(postController);
   
var Container = require("typedi").Container;
class beanfactory {
    constructor(container){}
    create() {
      console.log("beanfactory")
    }
}
class SugarFactor {
    constructor(container){}
    create() {
        console.log("SugarFactory")
    }
}
class WaterFactory{
    constructor(container){}
    create() {
        console.log("WaterFactory")
    }
}
class CoffeeMaker {
    constructor(container) {
        this.beanfactory = container.get("bean.factory");
        this.sugarFactory = container.get("sugar.factory");
        this.waterFactory = container.get("water.factory");
    }
    make() {
        this.beanfactory.create();
        this.sugarFactory.create();
        this.waterFactory.create();
    }
}
Container.set("bean.factory", new beanfactory(Container));
Container.set("sugar.factory", new SugarFactory(Container));
Container.set("water.factory", new WaterFactory(Container));
Container.set("coffee.maker", new CoffeeMaker(Container));
var coffeeMaker = Container.get("coffee.maker");
coffeeMaker.make();
   

typescript 使用

对于typescript 的使用我们可以利用ts提供的强大类型能力,以及注解可以编写强大的功能

  • 简单service注解
    项目结构
 
ts
└── service
    ├── SomeClass.ts
    └── demo.ts
   

demo.ts


import {Container} from "typedi";
import {SomeClass } from "./SomeClass"
let someClass = Container.get(SomeClass);
someClass.someMethod();
   

SomeClass.ts


import "reflect-Metadata";
import {Service} from "typedi";
@Service()
class SomeClass {
    someMethod() {
        console.log("call from service")
    }
}
export default SomeClass
export {
    SomeClass
}
   
import "reflect-Metadata"
import {Container, Inject, Service} from "typedi";
@Service()
class beanfactory {
    create() {
        console.log("beanfactory")
    }
}
@Service()
class SugarFactory {
    create() {
        console.log("SugarFactory")
    }
}
@Service()
class WaterFactory {
    create() {
        console.log("WaterFactory")
    }
}
@Service()
class CoffeeMaker {
    @Inject()
    beanfactory: beanfactory;
    @Inject()
    sugarFactory: SugarFactory;
    @Inject()
    waterFactory: WaterFactory;
    make() {
        this.beanfactory.create();
        this.sugarFactory.create();
        this.waterFactory.create();
    }
}
let coffeeMaker = Container.get(CoffeeMaker);
coffeeMaker.make();
   
  • 基于服务名的注入
import "reflect-Metadata"
import {Container, Service, Inject} from "typedi";
interface Factory {
    create(): void;
}
@Service("bean.factory")
class beanfactory implements Factory {
    create() {
        console.log("beanfactory")
    }
}
@Service("sugar.factory")
class SugarFactory implements Factory {
    create() {
        console.log("SugarFactory")
    }
}
@Service("water.factory")
class WaterFactory implements Factory {
    create() {
        console.log("WaterFactory")
    }
}
@Service("coffee.maker")
class CoffeeMaker {
    beanfactory: Factory;
    sugarFactory: Factory;
    @Inject("water.factory")
    waterFactory: Factory;
    constructor(@Inject("bean.factory") beanfactory: beanfactory,
                @Inject("sugar.factory") sugarFactory: SugarFactory) {
        this.beanfactory = beanfactory;
        this.sugarFactory = sugarFactory;
    }
    make() {
        this.beanfactory.create();
        this.sugarFactory.create();
        this.waterFactory.create();
    }
}
let coffeeMaker = Container.get<CoffeeMaker>("coffee.maker");
coffeeMaker.make();
   
  • 循环依赖的处理
    对于循环依赖我们需要指定类型,如下:
 
// Car.ts
@Service()
export class Car {
    @Inject()
    engine: Engine;
}
// Engine.ts
@Service()
export class Engine {
    @Inject()
    car: Car;
}
应该为:
// Car.ts
@Service()
export class Car {
    @Inject(type => Engine)
    engine: Engine;
}
// Engine.ts
@Service()
export class Engine {
    @Inject(type => Car)
    car: Car;
}
   
  • 注入继承
// Car.ts
@Service()
export abstract class Car {
    @Inject()
    engine: Engine;
}
// Engine.ts
@Service()
export class Bus extends Car {
    // you can call this.engine in this class
}
   
  • 服务组
    方便组合依赖的服务
 
// Factory.ts
import "reflect-Metadata"
import {Container, Service,Token} from "typedi";
export interface Factory {
    create(): any;
}
// FactoryToken.ts
export const FactoryToken = new Token<Factory>("factories");
// beanfactory.ts
@Service({ id: FactoryToken, multiple: true })
export class beanfactory implements Factory {
    create() {
        console.log("bean created");
    }
}
// SugarFactory.ts
@Service({ id: FactoryToken, multiple: true })
export class SugarFactory implements Factory {
    create() {
        console.log("sugar created");
    }
}
// WaterFactory.ts
@Service({ id: FactoryToken, multiple: true })
export class WaterFactory implements Factory {
    create() {
        console.log("water created");
    }
}
// app.ts
// Now you can get all factories in a single array
Container.import([
    beanfactory,
    SugarFactory,
    WaterFactory,
]);
const factories = Container.getMany(FactoryToken); // factories is Factory[]
factories.forEach(factory => factory.create());
 

说明

typedi 功能还是很强大,是web开发中的一个利器

参考资料

https://github.com/typestack/typedi
https://github.com/rongfengliang/typedi-learning

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐