During this brief tutorial, you will download and install Aglyph, build a simple Python application based on the MovieLister component discussed in Inversion of Control Containers and the Dependency Injection pattern, then modify the application to take advantage of Aglyph dependency injection. This process will allow you understand the Dependency Injection pattern in general, as well as the Aglyph approach to Dependency Injection.
This tutorial is a “whirlwind tour” of Aglyph that covers only the basics. Once you have completed the steps, please review the Aglyph API reference and the aglyph-context-1.0.0 DTD to understand the details.
The tutorial assumes that you are familiar with Python development in general, and that Python 2.5+ is already installed. For an introduction to Python, please see The Python Tutorial (also, the free Dive Into Python and Dive Into Python 3 books). Python can be downloaded from Python Programming Language – Official Website (or just use your preferred package installer, e.g. RPM).
Note
It is recommended, but not required, that you read the Inversion of Control Containers and the Dependency Injection pattern and Python Dependency Injection [PDF] articles before beginning this tutorial.
Download the latest distutils source or built distribution of Aglyph on SourceForge.
— OR —
Clone the Aglyph Mercurial repository from BitBucket.
If you downloaded the source distribution, unpack it into a temporary directory and then navigate into that directory. Issue the following command from a terminal:
python setup.py install
If you downloaded a built distribution, install it using the appropriate platform-specific tool.
If you cloned the repository from BitBucket, navigate into the root directory of the repository and issue the following command from a terminal:
python setup.py install
Verify that the installation was successful by importing the aglyph module from a Python interpreter. For example:
$ python
Python 2.7.2 (default, Jul 8 2011, 14:08:55)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import aglyph
The sample code for this tutorial can be downloaded here. Extract the ZIP archive to a temporary location and navigate into the application directory:
$ unzip movielisterapp-basic.zip
...
$ cd movielisterapp-basic
The movies.txt file is a simple colon-delimited text file that contains a number of title:director records, one per line:
The Colossus of Rhodes:Sergio Leone
Once Upon a Time in the West:Sergio Leone
THX 1138:George Lucas
American Graffiti:George Lucas
Once Upon a Time in America:Sergio Leone
Sixteen Candles:John Hughes
The Breakfast Club:John Hughes
Weird Science:John Hughes
Ferris Bueller's Day Off:John Hughes
This data file is read by a particular implementation of the MovieFinder class (ColonDelimitedMovieFinder), both of which can be found in the movies/finder.py module:
from movies.movie import Movie
class MovieFinder:
def find_all(self):
raise NotImplementedError()
class ColonDelimitedMovieFinder(MovieFinder):
def __init__(self, filename):
movies = []
f = open(filename)
for line in f:
(title, director) = line.strip().split(':')
movies.append(Movie(title, director))
f.close()
self._movies = movies
def find_all(self):
return self._movies
As you can see, each record is processed as a simple Movie data holder object. The movies/movie.py module holds the Movie class definition:
class Movie:
def __init__(self, title, director):
self.title = title
self.director = director
Finally, we have a MovieLister class (defined in the movies/lister.py module), which uses a ColonDelimitedMovieFinder to find the movies directed by a particular director:
from movies.finder import ColonDelimitedMovieFinder
class MovieLister:
def __init__(self):
self._finder = ColonDelimitedMovieFinder("movies.txt")
def movies_directed_by(self, director):
for movie in self._finder.find_all():
if (movie.director == director):
yield movie
The application can be executed using the app.py script, which simply asks a MovieLister for all movies directed by “Sergio Leone”:
$ python app.py
The Colossus of Rhodes
Once Upon a Time in the West
Once Upon a Time in America
Examine the MovieLister class (in the movies/lister.py module) again. There are three things to note:
As a consequence of (3), neither the concrete MovieFinder implementation nor the name/location of the data file can be changed without modifying MovieLister.
In other words, it is MovieLister that controls dependency resolution. It is this aspect of control that is being inverted (“Inversion of Control”) when we talk about Dependency Injection. Rather than having MovieLister be responsible for resolving its dependencies, we instead give control to some other object (an “assembler”), which has the responsibility of injecting dependencies into MovieLister.
The dependency injection approach provides several benefits:
Aglyph can inject dependencies using initializers – __init__ methods – or “factory” functions (type 2 “constructor” injection); or member variables, setter methods, and properties (type 3 “setter” injection).
In order to take advantage of type 2 “constructor” injection, the __init__ method or “factory” function must accept dependencies, which means we need to make some simple changes to movielisterapp...
As written, the basic application is somewhat change-resistant. For example, if we wish to support another implementation of MovieFinder (e.g. a CSVMovieFinder), then we would also need to change the MovieLister implementation.
A simple solution to this problem is to change MovieLister so that it can accept a MovieFinder at initialization time:
class MovieLister:
def __init__(self, finder):
self._finder = finder
def movies_directed_by(self, director):
for movie in self._finder.find_all():
if (movie.director == director):
yield movie
Next, we’ll add the CSVMovieFinder class definition to the movies/finder.py module:
import csv
from movies.movie import Movie
class MovieFinder:
def find_all(self):
raise NotImplementedError()
class ColonDelimitedMovieFinder(MovieFinder):
def __init__(self, filename):
movies = []
f = open(filename)
for line in f:
(title, director) = line.strip().split(':')
movies.append(Movie(title, director))
f.close()
self._movies = movies
def find_all(self):
return self._movies
class CSVMovieFinder(MovieFinder):
def __init__(self, filename):
movies = []
f = open(filename)
for (title, director) in csv.reader(f):
movies.append(Movie(title, director))
f.close()
self._movies = movies
def find_all(self):
return self._movies
The CSVMovieFinder expects a CSV filename. We’ll create movies.csv so that it contains the same records as the original movies.txt file:
The Colossus of Rhodes,Sergio Leone
Once Upon a Time in the West,Sergio Leone
THX 1138,George Lucas
American Graffiti,George Lucas
Once Upon a Time in America,Sergio Leone
Sixteen Candles,John Hughes
The Breakfast Club,John Hughes
Weird Science,John Hughes
Ferris Bueller's Day Off,John Hughes
Finally, we’ll change app.py so that the new CSVMovieFinder is used to initialize a MovieLister:
from movies.finder import CSVMovieFinder
from movies.lister import MovieLister
app = MovieLister(CSVMovieFinder("movies.csv"))
for movie in app.movies_directed_by("Sergio Leone"):
print(movie.title)
Running the application again should give us the same results:
$ python app.py
The Colossus of Rhodes
Once Upon a Time in the West
Once Upon a Time in America
The basic application is now more flexible: we can change the MovieFinder implementation without having to modify the MovieLister class definition. However, we are still required to modify app.py if we decide to change the MovieFinder implementation.
Note
An important aspect of Aglyph is that it is non-intrusive, meaning that it requires only minimal changes to your existing application code in order to provide dependency injection capabilities.
Notice that the changes made in this section, while adding flexibility to the application, did not require the use of Aglyph. In fact, as we add Aglyph dependency injection support in the next two sections, no further changes to the ``movies/lister.py``, ``movies/finder.py``, or ``movies/movie.py`` module need to be made.
Recall that Dependency Injection gives reponsibility for injecting dependencies to an an external object (called an “assembler”). In Aglyph, this “assembler” is defined by the aglyph.assembler.Assembler class.
An aglyph.assembler.Assembler requires a “context,” which is a collection of component definitions. A component definition is simply a description of some callable (an importable class or function), including its dependencies. Any component can itself be a dependency of any other component(s).
In Aglyph, a context is defined by the aglyph.context.Context class. A specialized subclass, aglyph.context.XMLContext, is provided to allow a context to be defined declaratively in an XML document. Such XML documents must conform to the aglyph-context-1.0.0 DTD.
The aglyph.context.Context class may also be used directly to define a context in pure Python. This approach requires the use of the aglyph.component.Component class, and (optionally) one of more of:
Changed in version 1.1.0: The preferred approach to programmatic configuration is now aglyph.binder.Binder, which is more succinct than using Context and Component directly.
Note
Using Context and Component directly is still supported (and, in fact, Binder uses them internally). You can view Binder as a “layer of abstraction” on top of Context and Component.
We will start by creating an Aglyph context for the movielisterapp application. For illustrative purposes, both an XML context and a pure-Python configuration will be created; in practice, one OR the other is recommended.
First, we’ll create the XML context document as movies-context.xml:
<?xml version="1.0" encoding="utf-8"?>
<context id="movies-context">
<component id="movies.finder.ColonDelimitedMovieFinder">
<init>
<arg><str>movies.txt</str></arg>
</init>
</component>
<component id="csv-finder" dotted-name="movies.finder.CSVMovieFinder">
<init>
<arg><str>movies.csv</str></arg>
</init>
</component>
<component id="movies.lister.MovieLister">
<init>
<arg reference="csv-finder"/>
</init>
</component>
</context>
Some interesting things to note here:
Notice that the movies.lister.MovieLister component is being injected with a reference to the csv-finder component, which describes an instance of movies.finder.CSVMovieFinder. We could easily change back to using movies.finder.ColonDelimitedMovieFinder by changing the reference.
Next, we’ll create the pure-Python configuration as the MoviesBinder class (a subclass of aglyph.binder.Binder) in the movies/__init__.py module:
from aglyph.binder import Binder
from movies.lister import MovieLister
from movies.finder import MovieFinder, CSVMovieFinder
class MoviesBinder(Binder):
def __init__(self):
super(MoviesBinder, self).__init__("movies-binder")
self.bind(MovieLister).init(MovieFinder)
self.bind(MovieFinder, to=CSVMovieFinder).init("movies.csv")
Some interesting things to note here:
Take a minute to examine the XML context and the pure-Python configuration; they will produce identical results. Each will inject the string “movies.csv” into a CSVMovieFinder, and then inject the CSVMovieFinder instance into a MovieLister.
Note
Aglyph assembles components according to a strategy (sometimes called a “scope”). Aglyph supports three strategies:
The assembly strategy for a component may be specified in the XML context or in pure Python. The following examples define a singleton component.
In XML:
<component id="the-object" dotted-name="builtins.object" strategy="singleton"/>
In Python:
Binder().bind("the-object", to=object, strategy="singleton")
# -or-
Component("the-object", "__builtin__.object", Strategy.SINGLETON)
If a strategy is not explicitly specified as part of the component definition, the default strategy is prototype.
Now that we have created a context for movielisterapp, it’s time to modify the app.py script to use dependency injection. To demonstrate the use of both an XML context and a pure-Python configuration, we’ll create two different “run” scripts.
The app_xmlcontext.py script will use the XML context:
from aglyph.assembler import Assembler
from aglyph.context import XMLContext
assembler = Assembler(XMLContext("movies-context.xml"))
app = assembler.assemble("movies.lister.MovieLister")
for movie in app.movies_directed_by("Sergio Leone"):
print(movie.title)
Warning
IronPython developers will need to create a slightly different app_xmlcontext.py script:
from aglyph.assembler import Assembler
from aglyph.compat.ipyetree import XmlReaderTreeBuilder
from aglyph.context import XMLContext
assembler = Assembler(XMLContext("movies-context.xml",
parser=XmlReaderTreeBuilder()))
app = assembler.assemble("movies.lister.MovieLister")
for movie in app.movies_directed_by("Sergio Leone"):
print(movie.title)
This is necessary because of the way that IronPython treats Unicode strings. See aglyph.compat.ipyetree for details.
This script creates an assembler with a context that is read from the movies-conext.xml XML document. Notice that we no longer need to create the CSVMovieFinder class directly; we have effectively separated the configuration of MovieLister from its use in the application.
Running the application produces the same results as usual:
$ python app_xmlcontext.py
The Colossus of Rhodes
Once Upon a Time in the West
Once Upon a Time in America
The app_binder.py script will use the pure-Python configuration:
from movies import MoviesBinder
from movies.lister import MovieLister
binder = MoviesBinder()
lister = binder.lookup(MovieLister)
for movie in lister.movies_directed_by("Sergio Leone"):
print(movie.title)
Here, we create the binder and then use it to look up the concrete implementation of MovieLister that we have configured.
Again, running the application produces the expected results:
$ python app_binder.py
The Colossus of Rhodes
Once Upon a Time in the West
Once Upon a Time in America
Now that the application is configured to use Aglyph for dependency injection, let’s make some changes to demonstrate application maintenance under Aglyph.
First, we note that both the ColonDelimitedMovieFinder and CSVMovieFinder classes read and parse their respective data files on every initialization. We don’t expect the data files to change very often, at least not during application runtime, so we’d prefer to only create either of these objects once. (For the moment, preted that movielisterapp is a useful application in which MovieFinder objects are used by more than just a MovieLister ;))
To accomplish this goal, we’ll modify the XML context so that the movies.finder.ColonDelimitedMovieFinder and csv-finder components use the singleton assembly strategy.
Recall that singleton assembly means only one object is created by Aglyph, and then cached. Subsequent assembly requests for the same component will return the cached object.
Also, we’ll change the movies.lister.MovieLister component so that it uses the original ColonDelimitedMovieFinder class instead of CSVMovieFinder.
The modified XML context looks like this:
<?xml version="1.0" encoding="utf-8"?>
<context id="movies-context">
<component id="movies.finder.ColonDelimitedMovieFinder"
strategy="singleton">
<init>
<arg><str>movies.txt</str></arg>
</init>
</component>
<component id="csv-finder" dotted-name="movies.finder.CSVMovieFinder"
strategy="singleton">
<init>
<arg><str>movies.csv</str></arg>
</init>
</component>
<component id="movies.lister.MovieLister">
<init>
<arg reference="movies.finder.ColonDelimitedMovieFinder"/>
</init>
</component>
</context>
Running the application still produces the expected results:
$ python app_xmlcontext.py
The Colossus of Rhodes
Once Upon a Time in the West
Once Upon a Time in America
To make the same change using the pure-Python configuration, the MoviesBinder configuration class would be changed like so:
from aglyph.binder import Binder
from movies.lister import MovieLister
from movies.finder import MovieFinder, ColonDelimitedMovieFinder
class MoviesBinder(Binder):
def __init__(self):
super(MoviesBinder, self).__init__("movies-binder")
self.bind(MovieLister).init(MovieFinder)
self.bind(MovieFinder, to=ColonDelimitedMovieFinder,
strategy="singleton").init("movies.txt")
Finally, running the application one last time produces the expected results:
$ python app_binder.py
Once Upon a Time in the West
Once Upon a Time in America
Note
The key point of this final exercise is that we were able to make “significant” changes without having to modify the application code itself. This is possible because we have separated the configuration of objects from their use; this is the goal of Depdendency Injection.
The final modified version of the movielisterapp application can be downloaded here as a reference.
There are many more context/configuration options available in Aglyph beyond those that have been presented in this tutorial, including support for type 2 “setter” injection using member variables, setter methods, and properties (which can also be combined with the type 3 “constructor” injection used in the movielisterapp sample application).
Suggested next steps: