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?

6
  • 1
    You need to clarify what you are talking about. Because DI and overloading is completely different concepts. Commented Jul 29, 2010 at 15:15
  • How do they appear similar to you? Maybe that will help us clear up any misunderstanding. Commented Jul 29, 2010 at 15:20
  • I clarified with my root cause of the doubt. Commented Jul 29, 2010 at 15:36
  • Are you meaning to ask about Dependency Injection vs. Method Overriding? Commented Jul 29, 2010 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. Commented Jul 29, 2010 at 15:58

3 Answers 3

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Jon! But can you please see my edit...and shed some light on that?
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;
    }
}

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.