简体   繁体   English

依赖注入太多了?

[英]Too much dependency injection?

I'm currently re-factoring a large application with quite a bit of spaghetti legacy code into something more structured and easy to maintain, and most importantly, testable. 我目前正在将一个大型应用程序与相当多的意大利面遗留代码重新分解为更结构化和易于维护的东西,最重要的是,可测试。 I can see that obviously injecting classes dependencies into them as opposed to mixing object creation with business logic makes writing unit tests much easier. 我可以看到,显然将类依赖注入到它们中,而不是将对象创建与业务逻辑混合使得编写单元测试变得更加容易。

I've read comments to the effect of "using dependency injection improperly causes more problems than it solves." 我已经读过评论“使用依赖注入不正确会导致更多问题而不是它解决的问题”。 What exactly does that mean? 这到底是什么意思呢? For such sophisticated verbiage, dependency injection seems like a pretty simple concept. 对于这种复杂的措辞,依赖注入似乎是一个非常简单的概念。 How do you abuse the idea of sending dependencies in through a constructor rather than instantiating them within the dependent class? 你如何滥用通过构造函数发送依赖的想法,而不是在依赖类中实例化它们? Why would the latter ever be preferable? 为什么后者会更好?

All I can see right now is that it makes isolating functionality to write tests and mocking objects extremely easy. 我现在所能看到的是它使隔离功能非常容易地编写测试和模拟对象。 It also seems to be making it ridiculously obvious when a class has too many responsibilities and points directly at classes that need to be refactored. 当一个类有太多的责任并直接指向需要重构的类时,它似乎也变得非常明显。 Am I getting carried away if I'm avoiding the use of the new keyword anywhere other than my DI container (with the exception of core PHP classes)? 如果我避免在我的DI容器以外的任何地方使用new关键字(核心PHP类除外),我会被带走吗?

I wouldn't worry about going overboard with dependency injection. 我不担心过度注入依赖注入。 You have already stated yourself some of the benefits that you have seen from adopting it (test-ability, encouraging good design re separation of concerns). 您已经说明了自己采用它所带来的一些好处(测试能力,鼓励良好的设计重新分离关注点)。

A criticism I have heard made of dependency injection (and IoC) is that it can introduce too much complexity. 我听说过依赖注入(和IoC)的批评是它会引入太多的复杂性。 While it is certainly possible to over-complicate things due to a bad implementation, I can not agree with this complaint as an intrinsic problem with dependency injection. 虽然由于实施不好而使事情过于复杂化当然是可能的,但我不能同意这种抱怨作为依赖注入的内在问题。

The thing to keep an eye on is that you choose the right level of abstraction and indirection for your dependencies that will give you the flexibility you need . 需要注意的是,您为依赖项选择了正确的抽象级别和间接级别,这将为您提供所需的灵活性 Too many levels of indirection can add complexity for no value, but I would argue that this is an orthogonal issue to dependency injection. 太多级别的间接可以增加没有价值的复杂性,但我认为这是依赖注入的正交问题。

See this related question for more possible downsides to dependency injection :) 有关依赖注入的更多可能缺点,请参阅此相关问题 :)


Regarding the issue of too many dependencies that you included in your original question prior to editing: 关于编辑前原始问题中包含的依赖项过多的问题:

Having many dependencies can be a code smell, and the things to focus on to address this should be: 拥有许多依赖项可能会产生代码异味,需要关注的重点应该是:

It sounds like you have already tackled part of your problem by following these principles. 听起来你已经按照这些原则解决了部分问题。 You should already look at whether your classes implement appropriate levels of abstraction. 您应该已经了解了您的类是否实现了适当的抽象级别。 For example, your class that needs to access multiple data models may benefit from accessing them through a facade or repository . 例如,需要访问多个数据模型的类可以通过Facade或存储库访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM