views:

154

answers:

3

Recently I was asked to express the DI in colloquial explanation.

I answered :

1) I am going to a hotel.I ordered food.The hotel management asks me to clean the plates and clean the tables.So here i am a client,I am responsible for managing the service (Instantiating,executing,disposing).But DI decouples such tasks so the service consumer no need not worry about controlling the life cycle of the service.

2) He also asked is there any microsoft API follows DI ?.I answered (This was my guess) In WCF you can create a Proxy using ChannelFactory that controls the life time of your factory.

for item (1) he said only 10% is correct

for item(2) he said that is factory pattern not dependency injection.

Actually what went wrong in my explanation (apart from my bad English) ? What is the real answers for those?

I am waiting for your valuable suggestions.

+2  A: 

I think your first answer is pretty spot on, although it's certainly possible to elaborate. It reminds me of one of my favorite answers here on Stack Overflow.

In my opinion, the most important DI pattern is Constructor Injection, but it's actually pretty hard to find examples in the BCL.

However, System.IO.StreamWriter is one example. Among many constructors, it has this one:

public StreamWriter(Stream stream);

Since Stream is an abstract class, it fits the description of a dependency perfectly, and it's being injected into the StreamWriter.

Your WCF example is an Abstract Factory. It's a very important pattern related to DI, but not DI in itself.

Mark Seemann
A: 

I think "colloquial" probably means to describe using non-technical language rather than give a real world example. So:

  1. Dependency Injection allows a user to provide their own solution to a problem and integrate it with a pre-built system. I can't think of a real world analogy to this at the moment.

  2. Think of a framework method that accepts an interface as a parameter and you probably have an example of dependency injection. I imagine there are plenty of examples in the Linq namespaces.

Phil
+3  A: 

1.

I think in your hotel example, it is more like you show up to eat and you're asked to first build the kitchen, fashion the plates, bowls and utensils, build a table and a chair, grow the wheat and vegetables, slaughter and butcher the meat, and then cook the meal and clean up.

My example would be a surgery/operation. The surgeon's responsibility is to perform the operation. That's it. She doesn't make the surgical instruments and she doesn't go and collect them all from the cabinets. When she shows up to perform the operation, all the tools are lined up and ready for her -- they've been provided from without and the surgeon doesn't care or need to know who put them there, as long as they are available and ready when she needs to her work.

Likewise in a given class, dependency injection means that any other component on which the class relies to do its work should be "injected" as a parameter to the constructor or by setting a property when the class is instantiated. The class is not concerned with finding or creating these things.

2.

I'd have mentioned that Microsoft Patterns & Practices/Enterprise Library supports DI with the Unity Application Block (an inversion-of-control container), and I think that the ASP.NET MVC library makes use of DI.

Jay