[英]Does it make sense to use DI container in front end javascript app
I am designing an app with React and its supporting libraries ecosystem.我正在设计一个带有 React 及其支持库生态系统的应用程序。 It's going to be a large app, with lots of services and helper modules.
这将是一个大型应用程序,具有许多服务和帮助程序模块。 To handle the dependency between them, does it make sense to use DI container.
为了处理它们之间的依赖关系,使用 DI 容器是否有意义。
[Update] [更新]
Please add missing problems/solutions we have available to prepare a good guideline to include/exclude DI container请添加我们可用的缺失问题/解决方案,以准备包含/排除 DI 容器的良好指南
Few problems that DI container tries to solve are DI 容器试图解决的几个问题是
It enables easy plug and play of modules Changes in constructor of the modules are limited to service registration它可以轻松实现模块的即插即用模块构造函数的变化仅限于服务注册
Without using DI container, I have following options不使用 DI 容器,我有以下选项
We use factory modules(initaliser) which just instantiate, this will enable plug, different module with same interface and wont require change where it is consumed.我们使用工厂模块(initaliser),它只是实例化,这将启用插件,具有相同接口的不同模块,并且不需要在消耗它的地方进行更改。
To make it singleton, service module will export instance of it, so that same instance is referred wherever its included.为了使其成为单例,服务模块将导出它的实例,以便在包含它的任何地方引用相同的实例。
One thing which will be missing though is single place(registry) where we can find all the dependencies of different modules.但是,将缺少的一件事是单个地方(注册表),我们可以在其中找到不同模块的所有依赖项。
Depends on the case.视情况而定。 If app is small then probably no.
如果应用程序很小,那么可能没有。 If app requirements changes often, you use SOLID approach and TS then it makes bit more sense.
如果应用需求经常变化,你使用 SOLID 方法和 TS 那么它更有意义。
It's like asking "Is car good to drive to work?".这就像问“开车上班好不好?”。 But we don't know how far is it for you to get from your home to work, do you have where to park, how much are you going spend on gas vs what is the public transportation ticket cost etc...
但是我们不知道您从家到工作地点有多远,您有没有停车的地方,您将在汽油上花多少钱以及公共交通票价是多少等等......
In general DI helps making code open for extensions but closed for modifications (Open/Close principle).一般来说,DI 有助于使代码对扩展开放,但对修改关闭(开放/关闭原则)。 It's always good idea to create high quality code.
创建高质量的代码总是一个好主意。 Sadly from busineess point of view DI in simple projects it will be waste of time and it will make steeper learning curve for frontend people.
可悲的是,从商业角度来看,简单项目中的 DI 会浪费时间,并且会使前端人员的学习曲线更加陡峭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.