Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Understanding IoC and Dependency Injection.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frank Rizzo - 24 Feb 2008 06:27 GMT
I am trying to understand IoC and Dependency Injection.  Some places
claim they are the same thing, others claim that they are different.
I've looked at code samples for both.

It seems like the technique is not much more than loading an object,
defined by an interface, on the fly from an outside assembly based on
the value in a configuration file (or some other place).  Is that all or
am I missing something?

Is this pretty much the same as COM technique 10 years ago that was
known as loading a Late Bound object against an interface?

Thanks.
Marc Gravell - 24 Feb 2008 10:37 GMT
Assuming this isn't a troll (sorry, but your post-count makes me very
suspicious), then that sounds about right... call it what you like:
factory, plugin, ioc, dependency injection, interface-first - all the
same fundamental idea.

> as loading a Late Bound object against an interface?
It surely can't be both late bound and using the interface at the same
time?

Marc
Dilip - 24 Feb 2008 16:17 GMT
> Assuming this isn't a troll (sorry, but your post-count makes me very
> suspicious),

That was unfair and completely uncalled for.  I have often wondered
the same thing and from the way post was worded I am pretty sure the
question was genuine.  Why not just answer the question, see how the
thread develops and then decide whether you want to contribute further
or not?
Marc Gravell - 24 Feb 2008 19:53 GMT
> That was unfair and completely uncalled for.
OK; I'll take that on the chin... you are correct and I apologise:
Frank - I'm sorry.

If I can briefly explain - the question seemed remarkably like one
very recently, so I checked history to see if it was the same poster,
and immediately google-groups showed me 40k-odd posts, not all of an
appreciable nature... unfortunately google-groops *doesn't* show the e-
mail address by default (you have to go through a captcha) - otherwise
I would have noticed the open e-mail address "none@none.net", so I was
incorrect to attribute the other threads to Frank.

So yes: I got it wrong; but I did also at least offer some input to
the question.

Marc
Frank Rizzo - 25 Feb 2008 05:53 GMT
>> That was unfair and completely uncalled for.
> OK; I'll take that on the chin... you are correct and I apologise:
[quoted text clipped - 7 lines]
> I would have noticed the open e-mail address "none@none.net", so I was
> incorrect to attribute the other threads to Frank.

That's fine.  Apology accepted.  Frank Rizzo, is, of course, not my real
name.  But I am not a troll, by my count (thunderbird makes it really
convinient), I have 59 posts in this forum under this nom de guerre.
The reason for none@none.net is spam.

> So yes: I got it wrong; but I did also at least offer some input to
> the question.
>
> Marc
Marc Gravell - 25 Feb 2008 07:35 GMT
> The reason for n...@none.net is spam.

Tell me about it... you don't want to /know/ how much spam my gmail
address gets, thanks mainly to using it for nntp... but it makes it
easier to dig out old posts when the common "how to..." questions come
up ;-p

Marc
Frank Rizzo - 25 Feb 2008 07:03 GMT
> Assuming this isn't a troll (sorry, but your post-count makes me very
> suspicious), then that sounds about right... call it what you like:
[quoted text clipped - 4 lines]
> It surely can't be both late bound and using the interface at the same
> time?

Huh?  In VB6, that was the only way to implement IoC.

dim oMySqlDataLoader as IDataLoader
dim oMsSqlDataLoader as IDataLoader

set oMySqlDataLoader = CreateObject("DataLoader.MySqlVariety")
set oMsSqlDataLoader = CreateObject("DataLoader.MicrosoftVariety")
Marc Gravell - 25 Feb 2008 07:22 GMT
> Huh?  In VB6, that was the only way to implement IoC.
>
[quoted text clipped - 3 lines]
> set oMySqlDataLoader = CreateObject("DataLoader.MySqlVariety")
> set oMsSqlDataLoader = CreateObject("DataLoader.MicrosoftVariety")

I think this is a trivial terminology thing...

It has been a few years since I really did COM, but IIR that isn't
strictly late-bound; late-bound denotes using IDispatch; however,
interface-binding denotes vtable-binding (unless the automation server
only supports IDispatch, which would be uncommon). I suspect that in
the above code, the "Set ... = " is actually using
IUnkown.QueryInterface to bind to the vtable of IDataLoader, and is
not late-bound. To do this using late binding you would dim "As
Object".

Marc
Jon Skeet [C# MVP] - 25 Feb 2008 07:39 GMT
> > Assuming this isn't a troll (sorry, but your post-count makes me very
> > suspicious), then that sounds about right... call it what you like:
[quoted text clipped - 12 lines]
> set oMySqlDataLoader = CreateObject("DataLoader.MySqlVariety")
> set oMsSqlDataLoader = CreateObject("DataLoader.MicrosoftVariety")

That's not IoC though. The point of IoC is that the objects are *told*
about their dependencies rather than creating the objects themselves.
So in unit tests you tell your objects to use some mock interface
implementations, and in the real code you tell your objects (usually
with a configuration file) to use some specific real implementation.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Marc Gravell - 25 Feb 2008 07:47 GMT
> That's not IoC though.

To be fair - it is a little hard to tell from just this (abbreviated)
context... replace "DataLoader.MySqlVariety" with sProcSig[*] and
you're pretty-much there for IoC with a factory-oriented
implementation...

Marc

[*]=well, it wouldn't be VB without a little Hungarian ;-p
Arne Vajhøj - 24 Feb 2008 16:42 GMT
> I am trying to understand IoC and Dependency Injection.  Some places
> claim they are the same thing, others claim that they are different.
> I've looked at code samples for both.

I think http://en.wikipedia.org/wiki/Dependency_injection is rather
accurate:

#Dependency injection (DI) refers to the process of supplying an
#external dependency to a software component and is a specific form of
#inversion of control where the concern being inverted is the process of
#obtaining the needed dependency.

> It seems like the technique is not much more than loading an object,
> defined by an interface, on the fly from an outside assembly based on
[quoted text clipped - 3 lines]
> Is this pretty much the same as COM technique 10 years ago that was
> known as loading a Late Bound object against an interface?

The concept as such is not new.

The way it is used in something like http://www.springframework.net/
has evolved.

Arne
Frank Rizzo - 25 Feb 2008 07:08 GMT
>> I am trying to understand IoC and Dependency Injection.  Some places
>> claim they are the same thing, others claim that they are different.
[quoted text clipped - 22 lines]
>
> Arne

Thanks for the explanation.  I always get initially confused when a well
known design, that was used in the past, all of a sudden becomes in
vogue and gets a new name.  Like "refactoring" used to be known as
"reshuffling your code to make it easier to understand".

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.