节点情况下的Typescript IOC [英] Typescript IOC in case of node
问题描述
我想知道您将如何使用Typescript IOC专门用于节点的应用程序.
I am wondering how would you use typescript IOC specifically node app.
对于外部模块基于基础的体系结构,该应用中没有任何类.纯粹的模块,因为我的应用严重依赖于node_modules.
In case of external module-based architecture there is no any classes in the app. Just pure modules because my app heavily depends on node_modules.
在这种情况下如何集成IOC解决方案?有什么想法吗?
How would I integrate IOC solution in such case? Any thoughts?
这是我要使用IOC的具体情况:
Here is my specific case I want to use IOC for:
我有猫鼬模型:
interface IStuffModel extends IStuff, mongoose.Document { }
var Stuff= mongoose.model<IStuffModel>('Stuff', Schemas.stuffSchema);
export = Stuff;
和相关的假类:
export class Stuff implements IStuff {
//do stuff
}
推荐答案
在这种情况下如何整合IOC解决方案
How would I integrate IOC solution in such case
这是我推荐的一个非常受欢迎的库: https://github.com/inversify/InversifyJS
Here is a very popular library that I recommend : https://github.com/inversify/InversifyJS
使用外部模块根本不会更改代码.代替
Using external modules doesn't change the code at all. Instead of
kernel.bind(new TypeBinding<FooBarInterface>("FooBarInterface", FooBar));
生产
您只有
import {ProdFooBar} from "./prodFooBar";
kernel.bind(new TypeBinding<FooBarInterface>("FooBarInterface", ProdFooBar));
测试
import {MockFooBar} from "./mockFooBar";
kernel.bind(new TypeBinding<FooBarInterface>("FooBarInterface", MockFooBar));
这篇关于节点情况下的Typescript IOC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!