<s>
In	O
software	B-General_Concept
engineering	I-General_Concept
,	O
dependency	B-Library
injection	I-Library
is	O
a	O
design	O
pattern	O
in	O
which	O
an	O
object	O
or	O
function	O
receives	O
other	O
objects	O
or	O
functions	O
that	O
it	O
depends	O
on	O
.	O
</s>
<s>
A	O
form	O
of	O
inversion	B-Language
of	I-Language
control	I-Language
,	O
dependency	B-Library
injection	I-Library
aims	O
to	O
separate	O
the	O
concerns	O
of	O
constructing	O
objects	O
and	O
using	O
them	O
,	O
leading	O
to	O
loosely	B-Application
coupled	I-Application
programs	O
.	O
</s>
<s>
Instead	O
,	O
the	O
receiving	O
'	O
client	B-Protocol
 '	O
(	O
object	O
or	O
function	O
)	O
is	O
provided	O
with	O
its	O
dependencies	B-Application
by	O
external	O
code	O
(	O
an	O
'	O
injector	O
 '	O
)	O
,	O
which	O
it	O
is	O
not	O
aware	O
of	O
.	O
</s>
<s>
Dependency	B-Library
injection	I-Library
helps	O
by	O
making	O
implicit	O
dependencies	B-Application
explicit	O
and	O
helps	O
solve	O
the	O
following	O
problems	O
:	O
</s>
<s>
In	O
addition	O
,	O
dependency	B-Library
injection	I-Library
is	O
used	O
for	O
keeping	O
code	O
in-line	O
with	O
the	O
dependency	B-Application
inversion	O
principle	O
.	O
</s>
<s>
Fundamentally	O
,	O
dependency	B-Library
injection	I-Library
consists	O
of	O
passing	O
parameters	O
to	O
a	O
method	O
.	O
</s>
<s>
Because	O
the	O
client	B-Protocol
does	O
not	O
build	O
or	O
find	O
the	O
service	O
itself	O
,	O
it	O
typically	O
only	O
needs	O
to	O
declare	O
the	O
interfaces	B-Application
of	O
the	O
services	O
it	O
uses	O
,	O
rather	O
than	O
their	O
concrete	O
implementations	O
.	O
</s>
<s>
An	O
example	O
of	O
inversion	B-Language
of	I-Language
control	I-Language
without	O
dependency	B-Library
injection	I-Library
is	O
the	O
template	O
method	O
pattern	O
,	O
where	O
polymorphism	B-Application
is	O
achieved	O
through	O
subclassing	O
.	O
</s>
<s>
In	O
contrast	O
,	O
dependency	B-Library
injection	I-Library
implements	O
inversion	B-Language
of	I-Language
control	I-Language
through	O
composition	B-Language
,	O
and	O
is	O
often	O
similar	O
to	O
the	O
strategy	O
pattern	O
.	O
</s>
<s>
A	O
difference	O
is	O
that	O
the	O
strategy	O
pattern	O
is	O
intended	O
for	O
dependencies	B-Application
that	O
are	O
interchangeable	O
throughout	O
an	O
object	O
's	O
lifetime	O
,	O
whereas	O
with	O
dependency	B-Library
injection	I-Library
typically	O
only	O
a	O
single	O
instance	O
of	O
a	O
dependency	B-Application
is	O
used	O
.	O
</s>
<s>
Dependency	B-Library
injection	I-Library
involves	O
four	O
roles	O
:	O
services	O
,	O
clients	O
,	O
interfaces	B-Application
and	O
injectors	O
.	O
</s>
<s>
In	O
turn	O
,	O
a	O
client	B-Protocol
is	O
any	O
class	O
which	O
uses	O
services	O
.	O
</s>
<s>
Any	O
object	O
can	O
be	O
a	O
service	O
or	O
a	O
client	B-Protocol
;	O
the	O
names	O
relate	O
only	O
to	O
the	O
role	O
the	O
objects	O
play	O
in	O
an	O
injection	O
.	O
</s>
<s>
The	O
same	O
object	O
may	O
even	O
be	O
both	O
a	O
client	B-Protocol
(	O
it	O
uses	O
injected	O
services	O
)	O
and	O
a	O
service	O
(	O
it	O
is	O
injected	O
into	O
other	O
objects	O
)	O
.	O
</s>
<s>
Upon	O
injection	O
,	O
the	O
service	O
is	O
made	O
part	O
of	O
the	O
client	B-Protocol
's	O
state	B-Application
,	O
available	O
for	O
use	O
.	O
</s>
<s>
Clients	O
should	O
not	O
know	O
how	O
their	O
dependencies	B-Application
are	O
implemented	O
,	O
only	O
their	O
names	O
and	O
API	B-Application
.	O
</s>
<s>
A	O
service	O
which	O
retrieves	O
emails	O
,	O
for	O
instance	O
,	O
may	O
use	O
the	O
IMAP	O
or	O
POP3	B-Protocol
protocols	O
behind	O
the	O
scenes	O
,	O
but	O
this	O
detail	O
is	O
likely	O
irrelevant	O
to	O
calling	O
code	O
that	O
merely	O
wants	O
an	O
email	O
retrieved	O
.	O
</s>
<s>
By	O
ignoring	O
implementation	O
details	O
,	O
clients	O
do	O
not	O
need	O
to	O
change	O
when	O
their	O
dependencies	B-Application
do	O
.	O
</s>
<s>
The	O
injector	O
,	O
sometimes	O
also	O
called	O
an	O
assembler	O
,	O
container	O
,	O
provider	O
or	O
factory	O
,	O
introduces	O
services	O
to	O
the	O
client	B-Protocol
.	O
</s>
<s>
The	O
injector	O
itself	O
may	O
be	O
many	O
objects	O
working	O
together	O
,	O
but	O
must	O
not	O
be	O
the	O
client	B-Protocol
,	O
as	O
this	O
would	O
create	O
a	O
circular	B-Language
dependency	I-Language
.	O
</s>
<s>
Because	O
dependency	B-Library
injection	I-Library
separates	O
how	O
objects	O
are	O
constructed	O
from	O
how	O
they	O
are	O
used	O
,	O
it	O
often	O
diminishes	O
the	O
importance	O
of	O
the	O
new	O
keyword	O
found	O
in	O
most	O
object-oriented	B-Language
languages	I-Language
.	O
</s>
<s>
Because	O
the	O
framework	B-Architecture
handles	O
creating	O
services	O
,	O
the	O
programmer	O
tends	O
to	O
only	O
directly	O
construct	O
value	O
objects	O
which	O
represents	O
entities	O
in	O
the	O
program	O
's	O
domain	O
(	O
such	O
as	O
an	O
Employee	O
object	O
in	O
a	O
business	O
app	O
or	O
an	O
Order	O
object	O
in	O
a	O
shopping	O
app	O
)	O
.	O
</s>
<s>
Car	O
engines	O
can	O
require	O
gas	O
,	O
diesel	O
or	O
electricity	O
,	O
but	O
this	O
detail	O
is	O
unimportant	O
to	O
the	O
client	B-Protocol
—	O
a	O
driver	O
—	O
who	O
only	O
cares	O
if	O
it	O
can	O
get	O
them	O
to	O
their	O
destination	O
.	O
</s>
<s>
A	O
basic	O
benefit	O
of	O
dependency	B-Library
injection	I-Library
is	O
decreased	O
coupling	B-Application
between	O
classes	O
and	O
their	O
dependencies	B-Application
.	O
</s>
<s>
By	O
removing	O
a	O
client	B-Protocol
's	O
knowledge	O
of	O
how	O
its	O
dependencies	B-Application
are	O
implemented	O
,	O
programs	O
become	O
more	O
reusable	O
,	O
testable	O
and	O
maintainable	O
.	O
</s>
<s>
This	O
also	O
results	O
in	O
increased	O
flexibility	O
:	O
a	O
client	B-Protocol
may	O
act	O
on	O
anything	O
that	O
supports	O
the	O
intrinsic	O
interface	O
the	O
client	B-Protocol
expects	O
.	O
</s>
<s>
More	O
generally	O
,	O
dependency	B-Library
injection	I-Library
reduces	O
boilerplate	O
code	O
,	O
since	O
all	O
dependency	B-Application
creation	O
is	O
handled	O
by	O
a	O
singular	O
component	O
.	O
</s>
<s>
Finally	O
,	O
dependency	B-Library
injection	I-Library
allows	O
concurrent	O
development	O
.	O
</s>
<s>
Plugins	B-Application
are	O
often	O
developed	O
by	O
third-parties	O
that	O
never	O
even	O
talk	O
to	O
developers	O
of	O
the	O
original	O
product	O
.	O
</s>
<s>
Many	O
of	O
dependency	B-Library
injection	I-Library
's	O
benefits	O
are	O
particularly	O
relevant	O
to	O
unit-testing	O
.	O
</s>
<s>
For	O
example	O
,	O
dependency	B-Library
injection	I-Library
can	O
be	O
used	O
to	O
externalize	O
a	O
system	O
's	O
configuration	O
details	O
into	O
configuration	O
files	O
,	O
allowing	O
the	O
system	O
to	O
be	O
reconfigured	O
without	O
recompilation	O
.	O
</s>
<s>
Similarly	O
,	O
because	O
dependency	B-Library
injection	I-Library
does	O
not	O
require	O
any	O
change	O
in	O
code	O
behavior	O
,	O
it	O
can	O
be	O
applied	O
to	O
legacy	O
code	O
as	O
a	O
refactoring	O
.	O
</s>
<s>
This	O
ease	O
of	O
testing	O
is	O
often	O
the	O
first	O
benefit	O
noticed	O
when	O
using	O
dependency	B-Library
injection	I-Library
.	O
</s>
<s>
Critics	O
of	O
dependency	B-Library
injection	I-Library
argue	O
that	O
it	O
:	O
</s>
<s>
Is	O
typically	O
implemented	O
with	O
reflection	O
or	O
dynamic	O
programming	O
,	O
hindering	O
IDE	B-Application
automation	O
.	O
</s>
<s>
Encourages	O
dependence	O
on	O
a	O
framework	B-Architecture
.	O
</s>
<s>
There	O
are	O
three	O
main	O
ways	O
in	O
which	O
a	O
client	B-Protocol
can	O
receive	O
injected	O
services	O
:	O
</s>
<s>
Constructor	O
injection	O
,	O
where	O
dependencies	B-Application
are	O
provided	O
through	O
a	O
client	B-Protocol
's	O
class	O
constructor	O
.	O
</s>
<s>
Setter	O
injection	O
,	O
where	O
the	O
client	B-Protocol
exposes	O
a	O
setter	O
method	O
which	O
accepts	O
the	O
dependency	B-Application
.	O
</s>
<s>
Interface	O
injection	O
,	O
where	O
the	O
dependency	B-Application
's	O
interface	O
provides	O
an	O
injector	O
method	O
that	O
will	O
inject	O
the	O
dependency	B-Application
into	O
any	O
client	B-Protocol
passed	O
to	O
it	O
.	O
</s>
<s>
In	O
some	O
frameworks	O
,	O
clients	O
do	O
not	O
need	O
to	O
actively	O
accept	O
dependency	B-Library
injection	I-Library
at	O
all	O
.	O
</s>
<s>
In	O
Java	B-Language
,	O
for	O
example	O
,	O
reflection	O
can	O
make	O
private	O
attributes	O
public	O
when	O
testing	O
and	O
inject	O
services	O
directly	O
.	O
</s>
<s>
In	O
the	O
following	O
Java	B-Language
example	O
,	O
the	O
Client	B-Protocol
class	O
contains	O
a	O
Service	O
member	B-Application
variable	I-Application
initialized	O
in	O
the	O
constructor	O
.	O
</s>
<s>
The	O
client	B-Protocol
directly	O
constructs	O
and	O
controls	O
which	O
service	O
it	O
uses	O
,	O
creating	O
a	O
hard-coded	O
dependency	B-Application
.	O
</s>
<s>
The	O
most	O
common	O
form	O
of	O
dependency	B-Library
injection	I-Library
is	O
for	O
a	O
class	O
to	O
request	O
its	O
dependencies	B-Application
through	O
its	O
constructor	O
.	O
</s>
<s>
This	O
ensures	O
the	O
client	B-Protocol
is	O
always	O
in	O
a	O
valid	O
state	B-Application
,	O
since	O
it	O
cannot	O
be	O
instantiated	O
without	O
its	O
necessary	O
dependencies	B-Application
.	O
</s>
<s>
By	O
accepting	O
dependencies	B-Application
through	O
a	O
setter	O
method	O
,	O
rather	O
than	O
a	O
constructor	O
,	O
clients	O
can	O
allow	O
injectors	O
to	O
manipulate	O
their	O
dependencies	B-Application
at	O
any	O
time	O
.	O
</s>
<s>
This	O
offers	O
flexibility	O
,	O
but	O
makes	O
it	O
difficult	O
to	O
ensure	O
that	O
all	O
dependencies	B-Application
are	O
injected	O
and	O
valid	O
before	O
the	O
client	B-Protocol
is	O
used	O
.	O
</s>
<s>
With	O
interface	O
injection	O
,	O
dependencies	B-Application
are	O
completely	O
ignorant	O
of	O
their	O
clients	O
,	O
yet	O
still	O
send	O
and	O
receive	O
references	O
to	O
new	O
clients	O
.	O
</s>
<s>
In	O
this	O
way	O
,	O
the	O
dependencies	B-Application
become	O
injectors	O
.	O
</s>
<s>
An	O
assembler	O
is	O
still	O
needed	O
to	O
introduce	O
the	O
client	B-Protocol
and	O
its	O
dependencies	B-Application
.	O
</s>
<s>
The	O
assembler	O
takes	O
a	O
reference	O
to	O
the	O
client	B-Protocol
,	O
casts	O
it	O
to	O
the	O
setter	O
interface	O
that	O
sets	O
that	O
dependency	B-Application
,	O
and	O
passes	O
it	O
to	O
that	O
dependency	B-Application
object	O
which	O
in	O
turn	O
passes	O
a	O
reference	O
to	O
itself	O
back	O
to	O
the	O
client	B-Protocol
.	O
</s>
<s>
For	O
interface	O
injection	O
to	O
have	O
value	O
,	O
the	O
dependency	B-Application
must	O
do	O
something	O
in	O
addition	O
to	O
simply	O
passing	O
back	O
a	O
reference	O
to	O
itself	O
.	O
</s>
<s>
This	O
could	O
be	O
acting	O
as	O
a	O
factory	O
or	O
sub-assembler	O
to	O
resolve	O
other	O
dependencies	B-Application
,	O
thus	O
abstracting	O
some	O
details	O
from	O
the	O
main	O
assembler	O
.	O
</s>
<s>
It	O
could	O
be	O
reference-counting	O
so	O
that	O
the	O
dependency	B-Application
knows	O
how	O
many	O
clients	O
are	O
using	O
it	O
.	O
</s>
<s>
If	O
the	O
dependency	B-Application
maintains	O
a	O
collection	O
of	O
clients	O
,	O
it	O
could	O
later	O
inject	O
them	O
all	O
with	O
a	O
different	O
instance	O
of	O
itself	O
.	O
</s>
<s>
The	O
simplest	O
way	O
of	O
implementing	O
dependency	B-Library
injection	I-Library
is	O
to	O
manually	O
arrange	O
services	O
and	O
clients	O
,	O
typically	O
done	O
at	O
the	O
program	O
's	O
root	O
,	O
where	O
execution	O
begins	O
.	O
</s>
<s>
Manual	O
construction	O
may	O
be	O
more	O
complex	O
and	O
involve	O
builders	O
,	O
factories	B-Application
,	O
or	O
other	O
construction	O
patterns	O
.	O
</s>
<s>
Manual	O
dependency	B-Library
injection	I-Library
is	O
often	O
tedious	O
and	O
error-prone	O
for	O
larger	O
projects	O
,	O
promoting	O
the	O
use	O
of	O
frameworks	O
which	O
automate	O
the	O
process	O
.	O
</s>
<s>
Manual	O
dependency	B-Library
injection	I-Library
becomes	O
a	O
dependency	B-Library
injection	I-Library
framework	B-Architecture
once	O
the	O
constructing	O
code	O
is	O
no	O
longer	O
custom	O
to	O
the	O
application	O
and	O
is	O
instead	O
universal	O
.	O
</s>
<s>
While	O
useful	O
,	O
these	O
tools	O
are	O
not	O
required	O
to	O
do	O
dependency	B-Library
injection	I-Library
.	O
</s>
<s>
Some	O
frameworks	O
,	O
like	O
Spring	B-Application
,	O
can	O
use	O
external	O
configuration	O
files	O
to	O
plan	O
program	O
composition	B-Language
:	O
</s>
<s>
Even	O
with	O
a	O
potentially	O
long	O
and	O
complex	O
object	O
graph	O
,	O
the	O
only	O
class	O
mentioned	O
in	O
code	O
is	O
the	O
entry	O
point	O
,	O
in	O
this	O
case	O
Client.Client	O
has	O
not	O
undergone	O
any	O
changes	O
to	O
work	O
with	O
Spring	B-Application
and	O
remains	O
a	O
POJO	B-Language
.	O
</s>
<s>
By	O
keeping	O
Spring-specific	O
annotations	O
and	O
calls	O
from	O
spreading	O
out	O
among	O
many	O
classes	O
,	O
the	O
system	O
stays	O
only	O
loosely	B-Application
dependent	O
on	O
Spring	B-Application
.	O
</s>
<s>
The	O
following	O
example	O
shows	O
an	O
AngularJS	B-Application
component	O
receiving	O
a	O
greeting	O
service	O
through	O
dependency	B-Library
injection	I-Library
.	O
</s>
<s>
Each	O
AngularJS	B-Application
application	O
contains	O
a	O
service	O
locator	O
responsible	O
for	O
the	O
construction	O
and	O
look-up	O
of	O
dependencies	B-Application
.	O
</s>
<s>
To	O
avoid	O
the	O
service	O
locator	O
antipattern	O
,	O
AngularJS	B-Application
allows	O
declarative	O
notation	O
in	O
HTML	O
templates	O
which	O
delegates	O
creating	O
components	O
to	O
the	O
injector	O
.	O
</s>
<s>
The	O
ng-controller	O
directive	O
triggers	O
the	O
injector	O
to	O
create	O
an	O
instance	O
of	O
the	O
controller	O
and	O
its	O
dependencies	B-Application
.	O
</s>
<s>
This	O
sample	O
provides	O
an	O
example	O
of	O
constructor	O
injection	O
in	O
C#	B-Application
.	O
</s>
