开发者

Interviewer asks: "What's the value of dependency injection?" [duplicate]

开发者 https://www.devze.com 2023-04-13 04:06 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: What 开发者_如何学Cis dependency injection?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

What 开发者_如何学Cis dependency injection?

I respond: "You don't need to use the new operator."

Interviewer's response: "<SIGH>. That's it?"

That's all I could come up with.

What would have been a more correct response if that is not correct?


It decouples a component from its external dependencies (e.g. other libraries, databases, etc.) allowing you to easily change them - even at runtime.

This can (for example) be useful in automated testing as you can inject mock objects via the public API.


Dependency injection decouples classes from the services they depend on, allowing you to register a set of services once and use them throughout your codebase.

This allows you to switch implementations non-intrusively, especially if the implementations are unit-tested.

It also allows you to put different service implementations with the same classes for different use-cases, such as web vs. GUI vs. testing.


The wikipedia article is a good reference: http://en.wikipedia.org/wiki/Dependency_injection.

Mainly, it reduces coupling and eases testing. You can conveniently unit test the class just by feeding it a mock instead of the real class. It's a good question in interviews to see if the candidate is familiar with TDD good practises.


Rather than thinking in terms of the words "dependency injection", think "list of factories". To use a real-world example, think of a catalog supply firm with thousands of different products. It would like to use a common scheme for keeping objects in inventory and reordering them when empty. It would be hard to run such a business if every product had its own independent means of ordering, and there had to be one person who knew how to order every product. That's not how businesses operate, however. Instead, there will be a file which contains for each product the information and procedures necessary to order it. Many products would use the same procedures, but a few products might require special procedures (e.g. call 815-555-6666 between 2pm and 3pm, ask for Steve, and ask him for widgets with blue end-caps, since the company normally puts on yellow end-caps).

Think of dependency injection as being the ability to include procurement instructions within the list of product types. Think of how smoothly the catalog firm can run with such ability, and how awkward it would be to have a firm of any size without it.

PS--"Dependency injection", like "Resource Allocation Is Initialization", is a phrase I dislike because its practical meaning has little to do with the words that comprise it. Most uses of DI don't seem to have much to do with "dependency", nor with "injecting" anything. I think "list of factories" is a word longer (but syllable shorter) and conveys the meaning much better. Incidentally, for RIAA, I'd substitute "object lifetime matches object scope".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号