mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-29 13:04:02 +03:00
Update external dependency provider docs
This commit is contained in:
parent
b5337fefa6
commit
9969bc4761
|
@ -295,6 +295,7 @@ class ExternalDependency(Provider):
|
||||||
raise Error('ExternalDependency provider expects to get class, ' +
|
raise Error('ExternalDependency provider expects to get class, ' +
|
||||||
'got {0} instead'.format(str(instance_of)))
|
'got {0} instead'.format(str(instance_of)))
|
||||||
self.instance_of = instance_of
|
self.instance_of = instance_of
|
||||||
|
self.provide = self.__call__
|
||||||
super(ExternalDependency, self).__init__()
|
super(ExternalDependency, self).__init__()
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 90 KiB After Width: | Height: | Size: 41 KiB |
|
@ -28,11 +28,11 @@ Example:
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
Class ``UserService`` is a part of some library. ``UserService`` has
|
Class ``UsersService`` is a part of some library. ``UsersService`` has
|
||||||
dependency on database connection, which can be satisfied with any
|
dependency on database connection, which can be satisfied with any
|
||||||
DBAPI 2.0 database connection. Being a self-sufficient library,
|
DBAPI 2.0 database connection. Being a self-sufficient library,
|
||||||
``UserService`` doesn't hardcode any kind of database management logic.
|
``UsersService`` doesn't hardcode any kind of database management logic.
|
||||||
Instead of this, ``UserService`` has external dependency, that has to
|
Instead of this, ``UsersService`` has external dependency, that has to
|
||||||
be satisfied by cleint's code, out of library's scope.
|
be satisfied by cleint's code, out of library's scope.
|
||||||
|
|
||||||
.. image:: /images/providers/external_dependency.png
|
.. image:: /images/providers/external_dependency.png
|
||||||
|
|
|
@ -2,13 +2,14 @@
|
||||||
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import contextlib
|
import contextlib
|
||||||
|
|
||||||
import dependency_injector.providers as providers
|
import dependency_injector.providers as providers
|
||||||
|
|
||||||
|
|
||||||
class UserService(object):
|
class UsersService(object):
|
||||||
"""Example class UserService.
|
"""Example class UsersService.
|
||||||
|
|
||||||
UserService has dependency on DBAPI 2.0 database connection.
|
UsersService has dependency on DBAPI 2.0 database connection.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, database):
|
def __init__(self, database):
|
||||||
|
@ -43,9 +44,9 @@ class UserService(object):
|
||||||
return cursor.fetchone()
|
return cursor.fetchone()
|
||||||
|
|
||||||
|
|
||||||
# Database and UserService providers:
|
# Database and UsersService providers:
|
||||||
database = providers.ExternalDependency(instance_of=sqlite3.dbapi2.Connection)
|
database = providers.ExternalDependency(instance_of=sqlite3.dbapi2.Connection)
|
||||||
users_service_factory = providers.Factory(UserService,
|
users_service_factory = providers.Factory(UsersService,
|
||||||
database=database)
|
database=database)
|
||||||
|
|
||||||
# Out of library's scope.
|
# Out of library's scope.
|
||||||
|
@ -57,10 +58,10 @@ database.provided_by(providers.Singleton(sqlite3.dbapi2.Connection,
|
||||||
detect_types=True,
|
detect_types=True,
|
||||||
isolation_level='EXCLUSIVE'))
|
isolation_level='EXCLUSIVE'))
|
||||||
|
|
||||||
# Creating UserService instance:
|
# Creating UsersService instance:
|
||||||
users_service = users_service_factory()
|
users_service = users_service_factory()
|
||||||
|
|
||||||
# Initializing UserService database:
|
# Initializing UsersService database:
|
||||||
users_service.init_database()
|
users_service.init_database()
|
||||||
|
|
||||||
# Creating test user and retrieving full information about him:
|
# Creating test user and retrieving full information about him:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user