mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-29 04:53:59 +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, ' +
|
||||
'got {0} instead'.format(str(instance_of)))
|
||||
self.instance_of = instance_of
|
||||
self.provide = self.__call__
|
||||
super(ExternalDependency, self).__init__()
|
||||
|
||||
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::
|
||||
|
||||
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
|
||||
DBAPI 2.0 database connection. Being a self-sufficient library,
|
||||
``UserService`` doesn't hardcode any kind of database management logic.
|
||||
Instead of this, ``UserService`` has external dependency, that has to
|
||||
``UsersService`` doesn't hardcode any kind of database management logic.
|
||||
Instead of this, ``UsersService`` has external dependency, that has to
|
||||
be satisfied by cleint's code, out of library's scope.
|
||||
|
||||
.. image:: /images/providers/external_dependency.png
|
||||
|
|
|
@ -2,13 +2,14 @@
|
|||
|
||||
import sqlite3
|
||||
import contextlib
|
||||
|
||||
import dependency_injector.providers as providers
|
||||
|
||||
|
||||
class UserService(object):
|
||||
"""Example class UserService.
|
||||
class UsersService(object):
|
||||
"""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):
|
||||
|
@ -43,9 +44,9 @@ class UserService(object):
|
|||
return cursor.fetchone()
|
||||
|
||||
|
||||
# Database and UserService providers:
|
||||
# Database and UsersService providers:
|
||||
database = providers.ExternalDependency(instance_of=sqlite3.dbapi2.Connection)
|
||||
users_service_factory = providers.Factory(UserService,
|
||||
users_service_factory = providers.Factory(UsersService,
|
||||
database=database)
|
||||
|
||||
# Out of library's scope.
|
||||
|
@ -57,10 +58,10 @@ database.provided_by(providers.Singleton(sqlite3.dbapi2.Connection,
|
|||
detect_types=True,
|
||||
isolation_level='EXCLUSIVE'))
|
||||
|
||||
# Creating UserService instance:
|
||||
# Creating UsersService instance:
|
||||
users_service = users_service_factory()
|
||||
|
||||
# Initializing UserService database:
|
||||
# Initializing UsersService database:
|
||||
users_service.init_database()
|
||||
|
||||
# Creating test user and retrieving full information about him:
|
||||
|
|
Loading…
Reference in New Issue
Block a user