views:

193

answers:

3

Though DI in interface driven...I am still not clear as to what exactly differentiates this from basic overloading concept. Any C# examples would be helpful.

EDIT : I read here the reason for my question that StreamReader can be seen as example of IoC/DI...how is this different totally from overloading? Or is it just semblance to DI and not entirely DI?

+14  A: 

They're completely different concepts.

Overloading: providing multiples methods with the same name (or constructors) that differ by the number and/or type of parameters.

Dependency Injection: giving components all the services they need to work with (e.g. authenticators, database connections etc) rather than letting them construct these dependencies themselves. DI encourages a clean separation between interfaces and implementation, and makes unit testing much easier (as you can mock/fake dependencies).

EDIT: I don't think I'd usually use StreamReader as a good example of dependency injection - in particular, it can create its own streams for you if you only specify a filename. Arguably the overloads with a Stream parameter are effectively allowing the stream dependency to be injected, but it's not what I'd normally consider as DI. The constructor is certainly an example of overloading - but the two are really unrelated.

Normally I'd think of DI in terms of services - things like authenticators, or potentially the next service in a chain (where a request goes through multiple stages, for example).

Jon Skeet
Thanks Jon! But can you please see my edit...and shed some light on that?
GilliVilla
Gillivilla: I've updated my answer below.
James Curran
+4  A: 

Overloading and Dependency Injection are completely independent ideas, there's really nothing in common except that you may take advantage of overloading while exercising dependency injection.

Overloading is a feature of the language where (for example) two methods can share the same name, but have different parameter lists. For example:

public Foo MakeFoo(int bar) { }
public Foo MakeFoo(double bar) { }
public Foo MakeFoo(Decimal bar) { }

Dependency Injection is a language-independent technique where you remove hidden dependencies that are generated within an object and instead pass them into the object. E.g.:

Transforming this:

// Foo has an implicit dependency on Bar
class Foo
{
    private Bar myBar;
    public Foo()
    {
        this.myBar = new Bar();  
    }
}

into this:

// Now Foo's dependency on Bar is explicit b/c it's being injected in the .ctor
class Foo
{
    private Bar myBar;
    public Foo(Bar aBar) 
    {
        this.myBar = aBar;
    }
}
Greg D
+2  A: 

It's hard to answer that, since the two concept really have nothng in common.

  • overloading: several methods (doing potenially completely different thing) share the same name (generally differeniated by different parameter lists)

  • Dependency Injection : Objects that are used by a method (or by a class) are not created within the method, but created outside and passed (injected) into it.

UPDATE (based on OP's UPDATE):

StreamReader is an example of DI, because it doesn't actually create the steam that it reads -- the stream is created elsewhere and passed into it's ctor. This allows it to work on streams of any form (disk files, string, network sockets etc)

James Curran