使用依赖注入有哪些缺点? [英] What are the downsides to using Dependency Injection?

查看:160
本文介绍了使用依赖注入有哪些缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在工作中引入DI作为模式,我们的主要开发人员之一想知道:使用依赖注入模式是什么 - 如果有的话 - 的缺点



注意我正在寻找一个 - 如果可能 - 详尽的列表,而不是主题的主观讨论。






澄清:我在谈论依赖注入模式(见这篇文章由Martin Fowler发表),而不是一个特定的框架,无论是基于XML(如Spring)还是代码(如Guice)或自动滚动。






修改:有关 / r / programming 的进一步讨论/辩论/辩论这里。

解决方案

几点:




  • DI增加复杂性,通常由于责任分离更多,增加课程数量,这并不总是有益的。

  • 您的代码将(有点)与您使用的依赖注入框架相结合(或更一般地说,您如何决定实施DI模式)

  • 执行类型解析的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.


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".


Edit: Some great further discussion / ranting / debate going on /r/programming here.

解决方案

A couple of points:

  • DI increases complexity, usually by increasing the number of classes since responsibilities are separated more, which is not always beneficial
  • Your code will be (somewhat) coupled to the dependency injection framework you use (or more generally how you decide to implement the DI pattern)
  • DI containers or approaches that perform type resolving generally incur a slight runtime penalty (very negligible, but it's there)

Generally, the benefit of decoupling makes each task simpler to read and understand, but increases the complexity of orchestrating the more complex tasks.

这篇关于使用依赖注入有哪些缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆