This link has been bookmarked by 607 people . It was first bookmarked on 05 Jul 2006, by morrita.
-
31 May 17
-
11 Jan 17
-
25 Oct 16
-
22 Sep 16
-
20 Sep 16
-
06 Sep 16
-
05 Aug 16
-
01 Aug 16
-
20 Jun 16
-
24 May 16
-
17 May 16
-
how to wire together different elements
-
the core problem is how do we assemble these plugins into an application
-
Inversion of Control
-
nversion of Control is too generic a term
-
Dependency Injection
-
The basic idea of the Dependency Injection is to have a separate object, an assembler, that populates a field in the lister class with an appropriate implementation for the finder interface
-
three main styles of dependency injection
-
Constructor Injection
-
uses a constructor to decide how to inject a finder implementation into the lister class
-
Setter Injection
-
Interface Injection
-
define and use interfaces for the injection
-
Service Locator
-
basic idea behind a service locator is to have an object that knows how to get hold of all of the services that an application might need
-
-
05 May 16
-
we want my lister to work with any implementation, and for that implementation to be plugged in at some later point, out of my hands. The problem is how can I make that link so that my lister class is ignorant of the implementation class, but can still talk to an instance to do its work.
-
the core problem is how do we assemble these plugins into an application?
-
the inversion is about how they lookup a plugin implementation. In my naive example the lister looked up the finder implementation by directly instantiating it. This stops the finder from being a plugin.
-
The approach that these containers use is to ensure that any user of a plugin follows some convention that allows a separate assembler module to inject the implementation into the lister.
-
The basic idea of the Dependency Injection is to have a separate object, an assembler, that populates a field in the lister class with an appropriate implementation for the finder interface
-
There are three main styles of dependency injection. The names I'm using for them are Constructor Injection, Setter Injection, and Interface Injection. If you read about this stuff in the current discussions about Inversion of Control you'll hear these referred to as type 1 IoC (interface injection), type 2 IoC (setter injection) and type 3 IoC (constructor injection).
-
The basic idea behind a service locator is to have an object that knows how to get hold of all of the services that an application might need.
-
27 Apr 16
-
18 Apr 16
-
11 Apr 16
uberbrodtDependency Injection pattern vs Service Locator/Registry
-
29 Feb 16
-
service and component.
-
component to mean a glob of software that's intended to be used, without change, by an application that is out of the control of the writers of the component.
-
A service is similar to a component in that it's used by foreign applications
-
a component to be used locally (think jar file, assembly, dll, or a source import)
-
A service will be used remotely through some remote interface, either synchronous or asynchronous (eg web service, messaging system, RPC, or socket.)
-
-
23 Nov 15
-
30 Oct 15
-
23 Oct 15
-
03 Oct 15
-
14 Sep 15
-
how to wire together different elements
-
without change
-
component
-
intended to be used
-
a glob of software
-
writers
-
out of the control
-
without change
-
source code
-
doesn't change
-
component's behavior
-
extending it
-
alter
-
component writers
-
foreign applications
-
service
-
it's used by
-
service
-
remotely
-
remote interface
-
synchronous or asynchronous
-
can be applied
-
components too
-
-
25 Aug 15
-
11 Jul 15
-
05 Jun 15
-
01 Jun 15
-
10 May 15
-
29 Apr 15
-
In the Java community there's been a rush of lightweight containers that help to assemble components from different projects into a cohesive application. Underlying these containers is a common pattern to how they perform the wiring, a concept they refer under the very generic name of "Inversion of Control". In this article I dig into how this pattern works, under the more specific name of "Dependency Injection", and contrast it with the Service Locator alternative. The choice between them is less important than the principle of separating configuration from use.
-
A common issue to deal with is how to wire together different elements: how do you fit together this web controller architecture with that database interface backing when they were built by different teams with little knowledge of each other.
-
A number of frameworks have taken a stab at this problem, and several are branching out to provide a general capability to assemble components from different layers. These are often referred to as lightweight containers, examples include PicoContainer, and Spring.
-
The topic of wiring elements together drags me almost immediately into the knotty terminology problems that surround the terms service and component. You find long and contradictory articles on the definition of these things with ease.
-
I use component to mean a glob of software that's intended to be used, without change, by an application that is out of the control of the writers of the component. By 'without change' I mean that the using application doesn't change the source code of the components, although they may alter the component's behavior by extending it in ways allowed by the component writers.
-
A service is similar to a component in that it's used by foreign applications. The main difference is that I expect a component to be used locally (think jar file, assembly, dll, or a source import). A service will be used remotely through some remote interface, either synchronous or asynchronous (eg web service, messaging system, RPC, or socket.)
-
The real point of this article is this finder object, or particularly how we connect the lister object with a particular finder object. The reason why this is interesting is that I want my wonderful
moviesDirectedBymethod to be completely independent of how all the movies are being stored. So all the method does is refer to a finder, and all that finder does is know how to respond to thefindAllmethod. -
In my book P of EAA, we described this situation as a Plugin. The implementation class for the finder isn't linked into the program at compile time, since I don't know what my friends are going to use. Instead we want my lister to work with any implementation, and for that implementation to be plugged in at some later point, out of my hands. The problem is how can I make that link so that my lister class is ignorant of the implementation class, but can still talk to an instance to do its work.
-
Expanding this into a real system, we might have dozens of such services and components. In each case we can abstract our use of these components by talking to them through an interface (and using an adapter if the component isn't designed with an interface in mind).
-
So the core problem is how do we assemble these plugins into an application? This is one of the main problems that this new breed of lightweight containers face, and universally they all do it using Inversion of Control.
-
When these containers talk about how they are so useful because they implement "Inversion of Control" I end up very puzzled. Inversion of control is a common characteristic of frameworks, so saying that these lightweight containers are special because they use inversion of control is like saying my car is special because it has wheels.
-
The question is: "what aspect of control are they inverting?" When I first ran into inversion of control, it was in the main control of a user interface. Early user interfaces were controlled by the application program. You would have a sequence of commands like "Enter name", "enter address"; your program would drive the prompts and pick up a response to each one. With graphical (or even screen based) UIs the UI framework would contain this main loop and your program instead provided event handlers for the various fields on the screen. The main control of the program was inverted, moved away from you to the framework.
-
For this new breed of containers the inversion is about how they lookup a plugin implementation. In my naive example the lister looked up the finder implementation by directly instantiating it. This stops the finder from being a plugin. The approach that these containers use is to ensure that any user of a plugin follows some convention that allows a separate assembler module to inject the implementation into the lister.
-
As a result I think we need a more specific name for this pattern. Inversion of Control is too generic a term, and thus people find it confusing. As a result with a lot of discussion with various IoC advocates we settled on the name Dependency Injection.
-
The basic idea of the Dependency Injection is to have a separate object, an assembler, that populates a field in the lister class with an appropriate implementation for the finder interface
-
There are three main styles of dependency injection. The names I'm using for them are Constructor Injection, Setter Injection, and Interface Injection. If you read about this stuff in the current discussions about Inversion of Control you'll hear these referred to as type 1 IoC (interface injection), type 2 IoC (setter injection) and type 3 IoC (constructor injection). I find numeric names rather hard to remember, which is why I've used the names I have here.
-
PicoContainer uses a constructor to decide how to inject a finder implementation into the lister class. For this to work, the movie lister class needs to declare a constructor that includes everything it needs injected.
class MovieLister...
public MovieLister(MovieFinder finder) { this.finder = finder; } -
The finder itself will also be managed by the pico container, and as such will have the filename of the text file injected into it by the container.
class ColonMovieFinder...
public ColonMovieFinder(String filename) { this.filename = filename; } -
The pico container then needs to be told which implementation class to associate with each interface, and which string to inject into the finder.
private MutablePicoContainer configureContainer() { MutablePicoContainer pico = new DefaultPicoContainer(); Parameter[] finderParams = {new ConstantParameter("movies1.txt")}; pico.registerComponentImplementation(MovieFinder.class, ColonMovieFinder.class, finderParams); pico.registerComponentImplementation(MovieLister.class); return pico; } -
This configuration code is typically set up in a different class. For our example, each friend who uses my lister might write the appropriate configuration code in some setup class of their own. Of course it's common to hold this kind of configuration information in separate config files. You can write a class to read a config file and set up the container appropriately. Although PicoContainer doesn't contain this functionality itself, there is a closely related project called NanoContainer that provides the appropriate wrappers to allow you to have XML configuration files.
-
Such a nano container will parse the XML and then configure an underlying pico container. The philosophy of the project is to separate the config file format from the underlying mechanism.
-
-
20 Apr 15
-
05 Apr 15
-
how to wire together different elements: how do you fit together this web controller architecture with that database interface backing when they were built by different teams with little knowledge of each other
-
A number of frameworks
-
are branching out to provide a general capability to assemble components from different layers
-
These are often referred to as
-
lightweight containers
-
mean a glob of software that's intended to be used, without change, by an application that is out of the control of the writers of the component
-
component
-
By 'without change' I mean that the using application doesn't change the source code of the components, although they may alter the component's behavior by extending it in ways allowed by the component writers.
-
A service is similar to a component in that it's used by foreign applications. The main difference is that I expect a component to be used locally
-
A service will be used remotely through some remote interface, either synchronous or asynchronous
-
I mostly use service in this article, but much of the same logic can be applied to local components too. Indeed often you need some kind of local component framework to easily access a remote service
-
The real point of this article is this finder object, or particularly how we connect the lister object with a particular finder object
-
want my wonderful
moviesDirectedBymethod to be completely independent of how all the movies are being stored -
what happens when my friends
-
would like a copy of my program
-
if they have a completely different form of storing their movie listing: a SQL database, an XML file
-
In this case we need a different class to grab that data
-
because I've defined a
MovieFinderinterface, this won't alter mymoviesDirectedBymethod. But I still need to have some way to get an instance of the right finder implementation into place -
The
MovieListerclass is dependent on both theMovieFinderinterface and upon the implementation. We would prefer it if it were only dependent on the interface, but then how do we make an instance to work with -
The implementation class for the finder isn't linked into the program at compile time
-
Instead we want my lister to work with any implementation, and for that implementation to be plugged in at some later point, out of my hands
-
how can I make that link so that my lister class is ignorant of the implementation class, but can still talk to an instance to do its work
-
Expanding this into a real system, we might have dozens of such services and components. In each case we can abstract our use of these components by talking to them through an interface
-
But if we wish to deploy this system in different ways, we need to use plugins to handle the interaction with these services so we can use different implementations in different deployments.
-
So the core problem is how do we assemble these plugins into an application
-
-
27 Mar 15
-
nterface injection
-
setter injection
-
onstructor injection
-
-
11 Mar 15
-
In the Java community there's been a rush of lightweight containers that help to assemble components from different projects into a cohesive application. Underlying these containers is a common pattern to how they perform the wiring, a concept they refer under the very generic name of "Inversion of Control".
-
Underlying these containers are a number of interesting design principles, things that go beyond both these specific containers and indeed the Java platform. Here I want to start exploring some of these principles. The examples I use are in Java, but like most of my writing the principles are equally applicable to other OO environments, particularly .NET.
-
I mostly use service in this article, but much of the same logic can be applied to local components too. Indeed often you need some kind of local component framework to easily access a remote service.
-
When these containers talk about how they are so useful because they implement "Inversion of Control" I end up very puzzled. Inversion of control is a common characteristic of frameworks, so saying that these lightweight containers are special because they use inversion of control is like saying my car is special because it has wheels.
-
-
27 Jan 15
-
25 Jan 15
-
20 Jan 15
-
13 Jan 15
-
29 Dec 14
-
14 Dec 14
-
01 Dec 14
-
26 Nov 14
-
The question is: "what aspect of control are they inverting?"
-
As a result I think we need a more specific name for this pattern. Inversion of Control is too generic a term, and thus people find it confusing. As a result with a lot of discussion with various IoC advocates we settled on the name Dependency Injection.
-
1 IoC (interface injection), type 2 IoC (setter injection) and type 3 IoC (constructor injection)
-
I find numeric names rather hard to remember, which is why I've used the names I have here.
-
-
21 Nov 14
-
01 Nov 14
-
12 Oct 14
-
13 Sep 14
-
19 Aug 14
-
17 Aug 14
-
For this new breed of containers the inversion is about how they lookup a plugin implementation.
-
he approach that these containers use is to ensure that any user of a plugin follows some convention that allows a separate assembler module to inject the implementation into the lister.
-
he other pattern you can use to do this is Service Locator
-
There are three main styles of dependency injection. The names I'm using for them are Constructor Injection, Setter Injection, and Interface Injection
-
-
02 Aug 14
-
I use component to mean a glob of software that's intended to be used, without change, by an application that is out of the control of the writers of the component.
-
A service is similar to a component in that it's used by foreign applications. The main difference is that I expect a component to be used locally (think jar file, assembly, dll, or a source import). A service will be used remotely through some remote interface, either synchronous or asynchronous (eg web service, messaging system, RPC, or socket.)
-
-
01 Aug 14
-
16 Jul 14
-
rush of lightweight containers
-
projects into a cohesive application.
-
perform the wiring
-
a common pattern to
-
name of "Dependency Injection"
-
Service Locator
-
separating configuration
-
heavyweight complexity i
-
how to wire together different elements
-
web controller architecture
-
interesting design principles
-
glob of software
-
component to mea
-
omponent's behavior by extending it
-
service is similar
-
t a component to be used locally
-
through some remote interface
-
bog of a real example.
-
at provides a list of movies
-
dandy
-
upon the implementation
-
MovieFinderinterfac -
only dependent on the interface
-
do we make an instance to work with?
-
situation as a Plugin.
-
with any implementation
-
an instance to do its work.
-
deploy this system in different ways
-
it using Inversion of Control.
-
car is special because it has wheels.
-
of control are they inverting
-
main control of a user interface
-
e application program
-
the lister looked up the finder implementation
-
Dependency Injection.
-
dependency from the application class to the plugin implementation
-
n assembl
-
have a separate objec
-
field in the lister class with an appropriate implementation
-
-
13 Jul 14
-
10 Jul 14
-
28 Jun 14
-
30 May 14
-
29 May 14
-
Inversion of Control
-
-
22 May 14
-
09 May 14
-
interfaces for the injection. Avalon is an example of a framework that uses this technique in places. I'll talk a bit more
-
he only way to break this dependency, another is to u
-
-
23 Apr 14
-
14 Apr 14
-
06 Apr 14
-
03 Apr 14
-
25 Mar 14
-
16 Mar 14
-
I use component to mean a glob of software that's intended to be used, without change,
-
, although they may alter the component's behavior by extending it in ways allowed by the component writer
-
using application doesn't change the source code of the components
-
I expect a component to be used locally
-
A service will be used remotely
-
Inversion of control is a common characteristic
-
Inversion of control is a common characteristic of frameworks
-
what aspect of control are they inverting?
-
how they lookup a plugin implementation
-
The approach that these containers use is to ensure that any user of a plugin follows some convention that allows a separate assembler module to inject the implementation into the lister.
-
Dependency Injection
-
Service Locator
-
Constructor Injection
-
Setter Injection
-
Interface Injection
-
-
01 Mar 14
-
02 Feb 14
-
18 Dec 13
-
17 Dec 13
-
20 Nov 13
-
13 Nov 13
-
25 Oct 13
-
21 Oct 13
-
18 Oct 13
-
25 Sep 13
-
16 Sep 13
-
21 Aug 13
-
14 Aug 13
-
04 Aug 13
-
17 Jul 13
-
A common issue to deal with is how to wire together different elements: how do you fit together this web controller architecture with that database interface backing when they were built by different teams with little knowledge of each other.
-
lightweight containers, examples include PicoContainer, and Spring.
-
I use component to mean a glob of software that's intended to be used, without change, by an application that is out of the control of the writers of the component.
-
By 'without change' I mean that the using application doesn't change the source code of the components, although they may alter the component's behavior by extending it in ways allowed by the component writers.
-
A service is similar to a component in that it's used by foreign applications. The main difference is that I expect a component to be used locally (think jar file, assembly, dll, or a source import). A service will be used remotely through some remote interface, either synchronous or asynchronous (eg web service, messaging system, RPC, or socket.)
-
The real point of this article is this finder object, or particularly how we connect the lister object with a particular finder object
-
The main control of the program was inverted, moved away from you to the framework.
-
inject the implementation into the lister.
-
-
10 Jul 13
-
25 Jun 13
-
22 Jun 13
-
31 May 13
-
The basic idea of the Dependency Injection is to have a separate object, an assembler, that populates a field in the lister class with an appropriate implementation for the finder interface, resulting in a dependency diagram along the lines of Figure 2
-
diagram
-
names I'm using for them are Constructor Injection, Setter Injection, and Interface Injection
-
three main styles of dependency injection
-
The philosophy of the project is to separate the config file format from the underlying mechanism.
-
The basic idea behind a service locator is to have an object that knows how to get hold of all of the services that an application might need.
-
-
27 Apr 13
-
18 Apr 13
-
12 Apr 13
-
16 Mar 13
-
A common issue to deal with is how to wire together different elements: how do you fit together this web controller architecture with that database interface backing when they were built by different teams with little knowledge of each other.
-
I use component to mean a glob of software that's intended to be used, without change, by an application that is out of the control of the writers of the component.
-
A service is similar to a component in that it's used by foreign applications. The main difference is that I expect a component to be used locally (think jar file, assembly, dll, or a source import).
-
finder
-
This particular piece of naivety I'm not going to fix, since it's just the scaffolding for the real point of this article.
-
The real point of this article is this finder object, or particularly how we connect the lister object with a particular finder object.
-
So all the method does is refer to a finder, and all that finder does is know how to respond to the
findAllmethod. I can bring this out by defining an interface for the finder. -
at some point I have to come up with a concrete class
-
public MovieLister()
-
new ColonDelimitedMovieFinder
-
If they also store their movie listings in a colon delimited text file called "movies1.txt" then everything is wonderful. If they have a different name for their movies file, then it's easy to put the name of the file in a properties file. But what if they have a completely different form of storing their movie listing
-
But I still need to have some way to get an instance of the right finder implementation into place.
-
The
MovieListerclass is dependent on both theMovieFinderinterface and upon the implementation. -
isn't linked into the program at compile
-
The problem is how can I make that link so that my lister class is ignorant of the implementation class, but can still talk to an instance to do its work.
-
But if we wish to deploy this system in different ways, we need to use plugins to handle the interaction with these services so we can use different implementations in different deployments.
-
Inversion of control is a common characteristic of frameworks, so saying that these lightweight containers are special because they use inversion of control is like saying my car is special because it has wheels.
-
what aspect of control
-
For this new breed of containers the inversion is about how they lookup a plugin implementation.
-
This stops the finder from being a plugin.
-
Inversion of Control is too generic a term, and thus people find it confusing. As a result with a lot of discussion with various IoC advocates we settled on the name Dependency Injection.
-
a separate object, an assembler, that populates a field in the lister class with an appropriate implementation
-
There are three main styles of dependency injection.
-
Constructor Injection, Setter Injection, and Interface Injection.
-
uses a constructor to decide how to inject a finder implementation
-
MovieFinder finder
-
finder itself will also be managed
-
String filename
-
which implementation class to associate with each interface, and which string to inject
-
new ConstantParameter("movies1.txt")
-
Parameter[]
-
Of course it's common to hold this kind of configuration information in separate config files. You can write a class to read a config file and set up the container appropriately.
-
NanoContainer
-
(MovieLister) pico.getComponentInstance(MovieLister.class)
-
framework for enterprise Java development
-
its developers tend to prefer setter injection
-
to accept the injection I define a setting method
-
setFinder(MovieFinder finder)
-
setFilename(String filename)
-
Spring supports configuration through XML files and also through code, but XML is the expected way to do it.
-
<beans>
-
<bean
-
property
-
The third injection technique is to define and use interfaces for the injection
-
I begin by defining an interface that I'll use to perform the injection through
-
interface InjectFinder
-
injectFinder(MovieFinder finder)
-
implements InjectFinder
-
InjectFinderFilename
-
implements MovieFinder, InjectFinderFilename
-
container.registerComponent("MovieLister", MovieLister.class); container.registerComponent("MovieFinder", ColonMovieFinder.class);
-
A new step is to register the injectors that will inject the dependent components.
-
registerInjectors()
-
container.registerInjector(InjectFinder.class, container.lookup("MovieFinder")); container.registerInjector(InjectFinderFilename.class, new FinderFilenameInjector());
-
For generic classes, such as the string, I use an inner class within the configuration code.
-
implements Injector
-
class FinderFilenameInjector implements Injector
-
it removes the dependency that the
MovieListerclass has on the concreteMovieFinder -
Injection isn't the only way to break this dependency, another is to use a service locator.
-
So a service locator for this application would have a method that returns a movie finder when one is needed.
-
In this case I'll use the ServiceLocator as a singleton Registry.
-
ServiceLocator.movieFinder()
-
soleInstance.movieFinder
-
Here I'm doing it in code, but it's not hard to use a mechanism that would read the appropriate data from a configuration file.
-
I've often heard the complaint that these kinds of service locators are a bad thing because they aren't testable because you can't substitute implementations for them. Certainly you can design them badly to get into this kind of trouble, but you don't have to.
-
I can easily create the locator with test implementations of my services.
-
the MovieLister is dependent on the full service locator class, even though it only uses one service
-
-
08 Mar 13
-
18 Feb 13
-
10 Feb 13
-
For this new breed of containers the inversion is about how they lookup a plugin implementation
-
Of course it's common to hold this kind of configuration information in separate config files.
-
The above example was static, in that the service locator class has methods for each of the services that you need. This isn't the only way of doing it, you can also make a dynamic service locator that allows you to stash any service you need into it and make your choices at runtime.
In this case, the service locator uses a map instead of fields for each of the services, and provides generic methods to get and load services.
-
Service Locator vs Dependency Injection
The fundamental choice is between Service Locator and Dependency Injection. The first point is that both implementations provide the fundamental decoupling that's missing in the naive example - in both cases application code is independent of the concrete implementation of the service interface. The important difference between the two patterns is about how that implementation is provided to the application class. With service locator the application class asks for it explicitly by a message to the locator. With injection there is no explicit request, the service appears in the application class - hence the inversion of control.
-
The key difference is that with a Service Locator every user of a service has a dependency to the locator. The locator can hide dependencies to other implementations, but you do need to see the locator. So the decision between locator and injector depends on whether that dependency is a problem
-
A common reason people give for preferring dependency injection is that it makes testing easier.
-
-
30 Jan 13
-
25 Jan 13
-
03 Jan 13
-
When these containers talk about how they are so useful because they implement "Inversion of Control" I end up very puzzled. Inversion of control is a common characteristic of frameworks, so saying that these lightweight containers are special because they use inversion of control is like saying my car is special because it has wheels.
The question, is what aspect of control are they inverting? When I first ran into inversion of control, it was in the main control of a user interface. Early user interfaces were controlled by the application program. You would have a sequence of commands like "Enter name", "enter address"; your program would drive the prompts and pick up a response to each one. With graphical (or even screen based) UIs the UI framework would contain this main loop and your program instead provided event handlers for the various fields on the screen. The main control of the program was inverted, moved away from you to the framework.
For this new breed of containers the inversion is about how they lookup a plugin implementation. In my naive example the lister looked up the finder implementation by directly instantiating it. This stops the finder from being a plugin. The approach that these containers use is to ensure that any user of a plugin follows some convention that allows a separate assembler module to inject the implementation into the lister.
As a result I think we need a more specific name for this pattern. Inversion of Control is too generic a term, and thus people find it confusing. As a result with a lot of discussion with various IoC advocates we settled on the name Dependency Injection.
-
-
30 Dec 12
Pavol BabinčákIn the Java community there's been a rush of lightweight containers that help to assemble components from different projects into a cohesive application. Underlying these containers is a common pattern to how they perform the wiring, a concept they refer under the very generic name of "Inversion of Control". In this article I dig into how this pattern works, under the more specific name of "Dependency Injection", and contrast it with the Service Locator alternative. The choice between them is less important than the principle of separating configuration from use.
-
21 Nov 12
-
13 Nov 12
-
11 Nov 12
-
07 Nov 12
-
24 Oct 12
-
15 Oct 12
-
11 Oct 12
-
08 Oct 12



Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.