使用依赖注入的缺点是什么? [英] What are the downsides to using Dependency Injection?
问题描述
我正在尝试将 DI 作为一种工作模式引入,我们的一位首席开发人员想知道:使用依赖注入模式有什么缺点?
I'm trying to introduce DI as a pattern here at work and one of our lead developers would like to know: What - if any - are the downsides to using the Dependency Injection pattern?
请注意,如果可能的话,我正在寻找一份详尽的清单,而不是对该主题的主观讨论.
Note I'm looking here for an - if possible - exhaustive list, not a subjective discussion on the topic.
澄清:我说的是依赖注入模式(参见 这篇文章 作者:Martin Fowler),不是一个特定的框架,无论是基于 XML(例如 Spring)还是基于代码(例如 Guice),或者自-滚动".
Clarification: I'm talking about the Dependency Injection pattern (see this article by Martin Fowler), not a specific framework, whether XML-based (such as Spring) or code-based (such as Guice), or "self-rolled".
编辑:一些很棒的进一步讨论/咆哮/辩论正在进行 /r/programming 在这里.
Edit: Some great further discussion / ranting / debate going on /r/programming here.
推荐答案
几点:
- DI 增加了复杂性,通常是通过增加类的数量,因为职责分离得更多,这并不总是有益的
- 您的代码将(在某种程度上)耦合到您使用的依赖注入框架(或者更一般地说,您决定如何实现 DI 模式)
- 执行类型解析的 DI 容器或方法通常会导致轻微的运行时损失(非常可以忽略不计,但确实存在)
通常,解耦的好处是使每个任务更易于阅读和理解,但增加了编排更复杂任务的复杂性.
Generally, the benefit of decoupling makes each task simpler to read and understand, but increases the complexity of orchestrating the more complex tasks.
这篇关于使用依赖注入的缺点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!