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?

Community
  • 1
  • 1
GilliVilla
  • 4,998
  • 11
  • 55
  • 96
  • 1
    You need to clarify what you are talking about. Because DI and overloading is completely different concepts. – Nix Jul 29 '10 at 15:15
  • How do they appear similar to you? Maybe that will help us clear up any misunderstanding. – jyoungdev Jul 29 '10 at 15:20
  • I clarified with my root cause of the doubt. – GilliVilla Jul 29 '10 at 15:36
  • Are you meaning to ask about Dependency Injection vs. Method *Overriding*? – Neil Jul 29 '10 at 15:42
  • Method OVERRIDING allows the behaviors of a class to be customized by creating another class that inherits it. This makes it easy to create a new class whose behavior is mostly similar to another class, but which has some custom features. Since any class can inherit from at most one other class, however, it is often necessary for classes to customize behavior of other classes without inheriting them. – supercat Jul 29 '10 at 15:58
  • You may want to refer to this question: http://stackoverflow.com/questions/130794/what-is-dependency-injection – Mark Seemann Jul 29 '10 at 17:05

3 Answers3

14

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
5

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
  • 43,259
  • 14
  • 84
  • 117
2

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
  • 101,701
  • 37
  • 181
  • 258