何时使用依赖注入 [英] When to use Dependency Injection

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

问题描述

最近几天我感到有一些感觉,依赖注入应该被称为我不能弥补我的想法 - 模式。我知道这可能听起来很愚蠢,但实际上是关于为什么要使用依赖注入(DI)的原因。通常说我应该使用DI来实现更高水平的松耦合,我得到那个部分。但是真的...我的数据库多久,一旦我的选择落在了MS SQL或者MySQL上,很少是对的?



有没有人有一些很引人注目的为什么DI是要去的原因?

解决方案

两个单词,单元测试

。 >

DI最令人信服的原因之一是允许更轻松的单元测试,而无需打数据库,并担心设置测试数据。


I've had a certain feeling these last couple of days that dependency-injection should really be called "I can't make up my mind"-pattern. I know this might sound silly, but really it's about the reasoning behind why I should use Dependency Injection (DI). Often it is said that I should use DI, to achieve a higher level of loose-coupling, and I get that part. But really... how often do I change my database, once my choice has fallen on MS SQL or MySQL .. Very rarely right?

Does anyone have some very compelling reasons why DI is the way to go?

解决方案

Two words, unit testing.

One of the most compelling reasons for DI is to allow easier unit testing without having to hit a database and worry about setting up 'test' data.

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

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