Update ExternalDependency provider docs

This commit is contained in:
Roman Mogilatov 2015-09-02 18:36:43 +03:00
parent a9eedc6760
commit 9326fdace4
3 changed files with 14 additions and 25 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

After

Width:  |  Height:  |  Size: 39 KiB

View File

@ -1,7 +1,7 @@
External dependency providers External dependency providers
----------------------------- -----------------------------
``ExternalDependency`` provider can be useful for development of ``di.ExternalDependency`` provider can be useful for development of
self-sufficient libraries / modules / applications that has required external self-sufficient libraries / modules / applications that has required external
dependencies. dependencies.
@ -22,10 +22,8 @@ in their application's architectures.
At the same time, you can be sure, that your external dependency will be At the same time, you can be sure, that your external dependency will be
satisfied with appropriate instance. satisfied with appropriate instance.
Example: Example:
.. note:: .. note::
Class ``UserService`` is a part of some library. ``UserService`` has Class ``UserService`` is a part of some library. ``UserService`` has

View File

@ -1,18 +1,10 @@
"""`ExternalDependency` providers example.""" """`ExternalDependency` providers example."""
from dependency_injector.providers import ExternalDependency
from dependency_injector.providers import Factory
from dependency_injector.providers import Singleton
from dependency_injector.injections import KwArg
from dependency_injector.injections import Attribute
# Importing SQLITE3 and contextlib.closing for working with cursors:
import sqlite3 import sqlite3
from contextlib import closing import contextlib
import dependency_injector as di
# Definition of example UserService:
class UserService(object): class UserService(object):
"""Example class UserService. """Example class UserService.
@ -29,7 +21,7 @@ class UserService(object):
def init_database(self): def init_database(self):
"""Initialize database, if it has not been initialized yet.""" """Initialize database, if it has not been initialized yet."""
with closing(self.database.cursor()) as cursor: with contextlib.closing(self.database.cursor()) as cursor:
cursor.execute(""" cursor.execute("""
CREATE TABLE IF NOT EXISTS users( CREATE TABLE IF NOT EXISTS users(
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
@ -39,31 +31,30 @@ class UserService(object):
def create(self, name): def create(self, name):
"""Create user with provided name and return his id.""" """Create user with provided name and return his id."""
with closing(self.database.cursor()) as cursor: with contextlib.closing(self.database.cursor()) as cursor:
cursor.execute('INSERT INTO users(name) VALUES (?)', (name,)) cursor.execute('INSERT INTO users(name) VALUES (?)', (name,))
return cursor.lastrowid return cursor.lastrowid
def get_by_id(self, id): def get_by_id(self, id):
"""Return user info by user id.""" """Return user info by user id."""
with closing(self.database.cursor()) as cursor: with contextlib.closing(self.database.cursor()) as cursor:
cursor.execute('SELECT id, name FROM users WHERE id=?', (id,)) cursor.execute('SELECT id, name FROM users WHERE id=?', (id,))
return cursor.fetchone() return cursor.fetchone()
# Database and UserService providers: # Database and UserService providers:
database = ExternalDependency(instance_of=sqlite3.dbapi2.Connection) database = di.ExternalDependency(instance_of=sqlite3.dbapi2.Connection)
users_service_factory = Factory(UserService, users_service_factory = di.Factory(UserService,
KwArg('database', database)) database=database)
# Out of library's scope. # Out of library's scope.
# #
# Setting database provider: # Setting database provider:
database.provided_by(Singleton(sqlite3.dbapi2.Connection, database.provided_by(di.Singleton(sqlite3.dbapi2.Connection,
KwArg('database', ':memory:'), database=':memory:',
KwArg('timeout', 30), timeout=30,
KwArg('detect_types', True), detect_types=True,
KwArg('isolation_level', 'EXCLUSIVE'), isolation_level='EXCLUSIVE'))
Attribute('row_factory', sqlite3.Row)))
# Creating UserService instance: # Creating UserService instance:
users_service = users_service_factory() users_service = users_service_factory()