是否有任何 JS/TS IoC 容器允许注入所有实现服务的集合?
我有 .NET 背景,使用 Autofac,我发现隐式关系类型非常有用。来自 Autofac 的文档:
例如,当 Autofac 注入 IEnumerable
类型的构造函数参数时, [(ITask 的集合)]它不会查找提供 IEnumerable 的组件。
相反,容器将找到 ITask 的所有实现并注入它们。
Javascript 世界里有类似的东西吗,还是我运气不好?
UPD 2022-04-11:我知道不可能通过“类型”来解决,因为没有类型;然而,JS 中的 IOC 容器通过使用“令牌”(符号、字符串等)来解决类型缺失的问题。
所以我正在寻找的是允许在同一令牌下注册多个提供者的 IoC 容器,然后解析由令牌组成的数组。
I come from the .NET background and, using Autofac, I found implicit relationship types pretty useful. From Autofac's documentation:
For example, when Autofac is injecting a constructor parameter of type IEnumerable<ITask> [(a collection of ITask)] it will not look for a component that supplies IEnumerable.
Instead, the container will find all implementations of ITask and inject all of them.
Is there anything similar in the Javascript world, or am I out of luck?
UPD 2022-04-11: I understand the fact that it's impossible to resolve by "type" because there is no type; however, IOC containers in JS work around the absence of types by using "tokens" instead (symbols, strings, etc.)
So what I'm looking for are IoC containers that allow registration of multiple providers under the same token, and then resolving an array of them by the token.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
C#是一种静态键入的语言,这意味着类型被视为一流的成员。您可以在运行时轻松参考变量的类型。 Typescript基于JavaScript,该JavaScript没有类型的概念。在运行时,从代码中删除了所有类型,这意味着任何代码都无法引用该类型。不幸的是,您是SOL。
我使用 inversifyjs 用于依赖注入,它是js/ts afaik的最全面的di包,您可以查看文档,看看它是否可以做类似的事情(尽管我怀疑)。
编辑:iNversify具有多喷射做您需要的。
C# is a statically typed language, which means types are treated as a first class member. You can easily reference types of your variables at runtime. TypeScript is based on JavaScript, which has no concept of types. At runtime, all types are removed from code, which means there's no way for any code to reference the type. Unfortunately you're SOL.
I use InversifyJS for dependency injection, it's the most fully featured DI package for JS/TS AFAIK, you can check out the docs to see if it can do something similar (although I doubt it).
EDIT: Inversify has a multi-inject option that'll do what you need.
InversifyJS 似乎支持我通过
container.getAll()
寻找的内容。https://doc.inversify.cloud/en/container_api.html
InversifyJS seems to support what I am looking for through
container.getAll()
.https://doc.inversify.cloud/en/container_api.html