2012-11-19 04:19:52 +00:00
Injector - Python dependency injection framework, inspired by Guice
2013-06-27 14:19:19 +00:00
===================================================================
2010-11-29 04:54:33 +00:00
2017-11-09 07:00:59 +00:00
[![image ](https://secure.travis-ci.org/alecthomas/injector.svg?branch=master )](https://travis-ci.org/alecthomas/injector)
2019-04-28 09:31:59 +00:00
[![Coverage Status ](https://coveralls.io/repos/github/alecthomas/injector/badge.svg?branch=master )](https://coveralls.io/github/alecthomas/injector?branch=master)
2012-11-13 20:37:40 +00:00
2012-11-19 04:19:52 +00:00
Introduction
2013-06-27 14:19:19 +00:00
------------
2010-11-29 04:54:33 +00:00
2013-06-27 14:19:19 +00:00
Dependency injection as a formal pattern is less useful in Python than in other languages, primarily due to its support for keyword arguments, the ease with which objects can be mocked, and its dynamic nature.
2010-11-25 14:04:56 +00:00
2013-06-27 14:19:19 +00:00
That said, a framework for assisting in this process can remove a lot of boiler-plate from larger applications. That's where Injector can help. It automatically and transitively provides keyword arguments with their values. As an added benefit, Injector encourages nicely compartmentalised code through the use of `Module` s.
2010-11-27 00:19:16 +00:00
2013-06-27 14:19:19 +00:00
While being inspired by Guice, it does not slavishly replicate its API. Providing a Pythonic API trumps faithfulness.
2010-11-25 14:33:37 +00:00
2013-09-25 22:40:23 +00:00
### How to get Injector?
2013-08-29 21:38:50 +00:00
2013-09-25 22:40:23 +00:00
* GitHub (code repository, issues): https://github.com/alecthomas/injector
2013-08-29 21:38:50 +00:00
2013-09-25 22:40:23 +00:00
* PyPI (installable, stable distributions): https://pypi.python.org/pypi/injector. You can install it using pip:
2013-08-29 21:38:50 +00:00
2016-09-08 01:59:52 +00:00
```bash
2013-09-25 22:40:23 +00:00
pip install injector
```
2013-03-19 01:22:21 +00:00
2013-09-25 22:40:23 +00:00
* Documentation: http://injector.readthedocs.org
2016-10-18 13:46:31 +00:00
* Change log: http://injector.readthedocs.io/en/latest/changelog.html
2013-03-19 01:22:21 +00:00
2019-04-05 13:56:23 +00:00
Injector works with CPython 3.5+ and PyPy 3 implementing Python 3.5+.
2013-06-27 14:19:19 +00:00
A Quick Example
---------------
2013-06-27 14:40:21 +00:00
2016-09-08 01:59:52 +00:00
```python
2013-06-27 14:36:08 +00:00
>>> from injector import Injector, inject
2017-05-26 12:21:20 +00:00
>>> class Inner:
2013-06-27 14:36:08 +00:00
... def __init__ (self):
... self.forty_two = 42
...
2017-05-26 12:21:20 +00:00
>>> class Outer:
2016-10-18 12:40:12 +00:00
... @inject
... def __init__ (self, inner: Inner):
2013-06-27 14:36:08 +00:00
... self.inner = inner
...
>>> injector = Injector()
>>> outer = injector.get(Outer)
>>> outer.inner.forty_two
42
2013-06-27 14:40:21 +00:00
2013-06-27 14:36:08 +00:00
```
2013-03-19 01:22:21 +00:00
2019-05-22 07:11:33 +00:00
Or with `dataclasses` if you like:
```python
from dataclasses import dataclass
from injector import Injector, inject
class Inner:
def __init__ (self):
self.forty_two = 42
@inject
@dataclass
class Outer:
inner: Inner
injector = Injector()
outer = injector.get(Outer)
print(outer.inner.forty_two) # Prints 42
```
2012-11-19 04:19:52 +00:00
A Full Example
2013-06-27 14:19:19 +00:00
--------------
Here's a full example to give you a taste of how Injector works:
2012-02-23 16:28:33 +00:00
2013-06-27 14:40:21 +00:00
2016-09-08 01:59:52 +00:00
```python
2019-06-16 18:46:33 +00:00
>>> from injector import Module, provider, Injector, inject, singleton
2013-06-27 14:40:21 +00:00
2013-06-27 14:36:08 +00:00
```
2012-02-23 16:28:33 +00:00
2013-06-27 14:19:19 +00:00
We'll use an in-memory SQLite database for our example:
2012-02-23 16:28:33 +00:00
2013-06-27 14:40:21 +00:00
2016-09-08 01:59:52 +00:00
```python
2013-06-27 14:36:08 +00:00
>>> import sqlite3
2013-06-27 14:40:21 +00:00
2013-06-27 14:36:08 +00:00
```
2012-02-23 16:28:33 +00:00
2013-09-02 00:06:29 +00:00
And make up an imaginary `RequestHandler` class that uses the SQLite connection:
2012-02-23 16:28:33 +00:00
2013-06-27 14:40:21 +00:00
2016-09-08 01:59:52 +00:00
```python
2017-05-26 12:21:20 +00:00
>>> class RequestHandler:
2016-10-18 12:40:12 +00:00
... @inject
... def __init__ (self, db: sqlite3.Connection):
2013-06-27 14:36:08 +00:00
... self._db = db
...
... def get(self):
... cursor = self._db.cursor()
... cursor.execute('SELECT key, value FROM data ORDER by key')
... return cursor.fetchall()
2013-06-27 14:40:21 +00:00
2013-06-27 14:36:08 +00:00
```
2012-02-23 16:28:33 +00:00
2019-06-14 23:05:42 +00:00
Next, for the sake of the example, we'll create a configuration type:
2012-02-23 16:28:33 +00:00
2013-06-27 14:40:21 +00:00
2016-09-08 01:59:52 +00:00
```python
2019-06-14 23:05:42 +00:00
>>> class Configuration:
... def __init__ (self, connection_string):
... self.connection_string = connection_string
2019-06-14 23:17:55 +00:00
2013-06-27 14:36:08 +00:00
```
2012-02-23 16:28:33 +00:00
2019-06-14 23:05:42 +00:00
Next, we bind the configuration to the injector, using a module:
2013-04-30 14:16:39 +00:00
2013-06-27 14:40:21 +00:00
2016-09-08 01:59:52 +00:00
```python
2013-06-27 14:36:08 +00:00
>>> def configure_for_testing(binder):
2019-06-14 23:05:42 +00:00
... configuration = Configuration(':memory:')
2013-06-27 14:36:08 +00:00
... binder.bind(Configuration, to=configuration, scope=singleton)
2013-06-27 14:40:21 +00:00
2013-06-27 14:36:08 +00:00
```
2013-04-30 14:16:39 +00:00
2013-09-02 00:06:29 +00:00
Next we create a module that initialises the DB. It depends on the configuration provided by the above module to create a new DB connection, then populates it with some dummy data, and provides a `Connection` object:
2012-02-23 16:28:33 +00:00
2013-06-27 14:40:21 +00:00
2016-09-08 01:59:52 +00:00
```python
2013-06-27 14:36:08 +00:00
>>> class DatabaseModule(Module):
... @singleton
2016-10-25 14:15:24 +00:00
... @provider
... def provide_sqlite_connection(self, configuration: Configuration) -> sqlite3.Connection:
2019-06-14 23:05:42 +00:00
... conn = sqlite3.connect(configuration.connection_string)
2013-06-27 14:36:08 +00:00
... cursor = conn.cursor()
... cursor.execute('CREATE TABLE IF NOT EXISTS data (key PRIMARY KEY, value)')
... cursor.execute('INSERT OR REPLACE INTO data VALUES ("hello", "world")')
... return conn
2013-06-27 14:40:21 +00:00
2013-06-27 14:36:08 +00:00
```
2012-02-23 16:28:33 +00:00
2013-06-27 14:19:19 +00:00
(Note how we have decoupled configuration from our database initialisation code.)
2012-02-23 16:28:33 +00:00
2013-09-02 00:06:29 +00:00
Finally, we initialise an `Injector` and use it to instantiate a `RequestHandler` instance. This first transitively constructs a `sqlite3.Connection` object, and the Configuration dictionary that it in turn requires, then instantiates our `RequestHandler` :
2012-02-23 16:28:33 +00:00
2013-06-27 14:40:21 +00:00
2016-09-08 01:59:52 +00:00
```python
2013-06-27 14:36:08 +00:00
>>> injector = Injector([configure_for_testing, DatabaseModule()])
>>> handler = injector.get(RequestHandler)
>>> tuple(map(str, handler.get()[0])) # py3/py2 compatibility hack
('hello', 'world')
2013-06-27 14:40:21 +00:00
2013-06-27 14:36:08 +00:00
```
2012-02-23 16:28:33 +00:00
2014-01-09 10:02:13 +00:00
We can also verify that our `Configuration` and `SQLite` connections are indeed singletons within the Injector:
2012-02-23 16:28:33 +00:00
2013-06-27 14:40:21 +00:00
2016-09-08 01:59:52 +00:00
```python
2013-06-27 14:36:08 +00:00
>>> injector.get(Configuration) is injector.get(Configuration)
True
>>> injector.get(sqlite3.Connection) is injector.get(sqlite3.Connection)
True
2013-06-27 14:40:21 +00:00
2013-06-27 14:36:08 +00:00
```
2012-02-23 16:28:33 +00:00
2013-06-27 14:19:19 +00:00
You're probably thinking something like: "this is a large amount of work just to give me a database connection", and you are correct; dependency injection is typically not that useful for smaller projects. It comes into its own on large projects where the up-front effort pays for itself in two ways:
2012-02-23 16:28:33 +00:00
2013-06-27 14:37:42 +00:00
1. Forces decoupling. In our example, this is illustrated by decoupling our configuration and database configuration.
2013-09-02 00:06:29 +00:00
2. After a type is configured, it can be injected anywhere with no additional effort. Simply `@inject` and it appears. We don't really illustrate that here, but you can imagine adding an arbitrary number of `RequestHandler` subclasses, all of which will automatically have a DB connection provided.
2012-02-23 16:28:33 +00:00
2012-11-19 04:19:52 +00:00
Footnote
2013-06-27 14:19:19 +00:00
--------
2010-11-27 00:19:16 +00:00
This framework is similar to snake-guice, but aims for simplification.
2010-11-25 14:33:37 +00:00
2013-08-10 14:47:52 +00:00
© Copyright 2010-2013 to Alec Thomas, under the BSD license