views:

160

answers:

4

One of my friend (he is a .NET/C++ developer, as I am) asked me:

Dependency injection: do we all need to know that? Why?

Could you please tell:

  1. Your opinion: do we really need to know that pattern and how to implement it?
  2. Good reference (link) that could explain "why?"

Thank you a lot.

P.S. I am understanding that I am asking a lot... but I can't find good reference myself...

+4  A: 

Dependency injection is a tool for creating loosely coupled code. As such it isn't mandatory, but it will make changes, refactoring and testing easier. If you want a thorough discussion of DI and how it can be applied I recommend the upcoming book Dependency Injection in .NET. Several chapters are available for download to get you started. While the examples in the book are all C#, the ideas are universal.

Brian Rasmussen
+2  A: 

Here is a really nice reference why Dependency injection is crucial for writing (unit) testable code:

http://www.youtube.com/watch#!v=wEhu57pih5w&feature=related

(The Clean Code Talks -- Unit Testing)

Doc Brown
+3  A: 

Dependency Injection is a tool that helps a programmer or architect design modular, independent, loosely coupled, and easily testable code. It is not a mandatory requirement, however it does tend to make software development simpler as it alleviates a lot of dependency management issues that would otherwise arise. As it decouples your components from their dependencies, it also makes it much easier to unit test those components in isolation from the rest of a larger product.

About a year ago, I posted an answer to an IoC (Inversion of Control) question here on SO. You may find the answer useful, as the concepts also apply to Dependency Injection, as IoC is simply a means of achieving DI.

Can anyone explain to me, at length, how to use IOC containers?

Additionally, here are some good references explaining DI in more detail:

jrista
+1  A: 

Good comments are posted here, but there are no clear answer on the question "Your opinion: do we really need to know that pattern and how to implement it?"

From my perspective it should be "yes" if at least one of option is applied to you:

  • you need to write (automated) unit-tests for application;
  • you need to decouple components of your application;
  • you want to have ability to easily change source of data.

Answer on question "Why?" are listed in suggested links.

Many thanks for your help.

Budda