Understanding IoC and Dependency Injection.

F

Frank Rizzo

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

Marc Gravell

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
 
D

Dilip

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?
 
A

Arne Vajhøj

Frank said:
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
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?

The concept as such is not new.

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

Arne
 
M

Marc Gravell

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 "(e-mail address removed)", 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
 
F

Frank Rizzo

Marc said:
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 "(e-mail address removed)", 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 (e-mail address removed) is spam.
 
F

Frank Rizzo

Marc said:
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.

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")
 
F

Frank Rizzo

Arne said:
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.


The concept as such is not new.

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

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".
 
M

Marc Gravell

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

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
 
M

Marc Gravell

The reason for (e-mail address removed) 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
 
J

Jon Skeet [C# MVP]

Frank Rizzo said:
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")

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

Marc Gravell

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top