mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 17:47:02 +03:00
Update some examples
This commit is contained in:
parent
79bde335fa
commit
a42fefacef
64
README.rst
64
README.rst
|
@ -70,58 +70,62 @@ Examples
|
||||||
import dependency_injector as di
|
import dependency_injector as di
|
||||||
|
|
||||||
|
|
||||||
class ObjectA(object):
|
class UsersService(object):
|
||||||
"""Example class ObjectA, that has dependency on database."""
|
"""Users service, that has dependency on database."""
|
||||||
|
|
||||||
def __init__(self, db):
|
def __init__(self, db):
|
||||||
"""Initializer."""
|
"""Initializer."""
|
||||||
self.db = db
|
self.db = db
|
||||||
|
|
||||||
|
|
||||||
class ObjectB(object):
|
class AuthService(object):
|
||||||
"""Example class ObjectB, that has dependencies on ObjectA and database."""
|
"""Auth service, that has dependencies on users service and database."""
|
||||||
|
|
||||||
def __init__(self, a, db):
|
def __init__(self, db, users_service):
|
||||||
"""Initializer."""
|
"""Initializer."""
|
||||||
self.a = a
|
|
||||||
self.db = db
|
self.db = db
|
||||||
|
self.users_service = users_service
|
||||||
|
|
||||||
|
|
||||||
class Catalog(di.AbstractCatalog):
|
class Services(di.AbstractCatalog):
|
||||||
"""Catalog of providers."""
|
"""Catalog of service providers."""
|
||||||
|
|
||||||
database = di.Singleton(sqlite3.Connection,
|
database = di.Singleton(sqlite3.connect, ':memory:')
|
||||||
database=':memory:')
|
""":type: () -> sqlite3.Connection"""
|
||||||
""":type: (di.Provider) -> sqlite3.Connection"""
|
|
||||||
|
|
||||||
object_a_factory = di.Factory(ObjectA,
|
users = di.Factory(UsersService,
|
||||||
db=database)
|
db=database)
|
||||||
""":type: (di.Provider) -> ObjectA"""
|
""":type: () -> UsersService"""
|
||||||
|
|
||||||
object_b_factory = di.Factory(ObjectB,
|
auth = di.Factory(AuthService,
|
||||||
a=object_a_factory,
|
db=database,
|
||||||
db=database)
|
users_service=users)
|
||||||
""":type: (di.Provider) -> ObjectB"""
|
""":type: () -> AuthService"""
|
||||||
|
|
||||||
|
|
||||||
# Catalog static provides.
|
# Retrieving catalog providers:
|
||||||
a1, a2 = Catalog.object_a_factory(), Catalog.object_a_factory()
|
users_service = Services.users()
|
||||||
b1, b2 = Catalog.object_b_factory(), Catalog.object_b_factory()
|
auth_service = Services.auth()
|
||||||
|
|
||||||
assert a1 is not a2
|
# Making some asserts:
|
||||||
assert b1 is not b2
|
assert users_service.db is auth_service.db is Services.database()
|
||||||
assert a1.db is a2.db is b1.db is b2.db is Catalog.database()
|
assert isinstance(auth_service.users_service, UsersService)
|
||||||
|
assert users_service is not Services.users()
|
||||||
|
assert auth_service is not Services.auth()
|
||||||
|
|
||||||
|
|
||||||
# Example of inline injections.
|
# Making some "inline" injections:
|
||||||
@di.inject(a=Catalog.object_a_factory)
|
@di.inject(users_service=Services.users)
|
||||||
@di.inject(b=Catalog.object_b_factory)
|
@di.inject(auth_service=Services.auth)
|
||||||
@di.inject(database=Catalog.database)
|
@di.inject(database=Services.database)
|
||||||
def example(a, b, database):
|
def example(users_service, auth_service, database):
|
||||||
"""Example callback."""
|
"""Example callback."""
|
||||||
assert a.db is b.db is database is Catalog.database()
|
assert users_service.db is auth_service.db
|
||||||
|
assert auth_service.db is database
|
||||||
|
assert database is Services.database()
|
||||||
|
|
||||||
|
|
||||||
|
# Making a call of decorated callback:
|
||||||
example()
|
example()
|
||||||
|
|
||||||
You can get more *Dependency Injector* examples in ``/examples`` directory on
|
You can get more *Dependency Injector* examples in ``/examples`` directory on
|
||||||
|
|
|
@ -39,7 +39,7 @@ from .utils import ensure_is_catalog_bundle
|
||||||
from .errors import Error
|
from .errors import Error
|
||||||
|
|
||||||
|
|
||||||
VERSION = '0.10.0'
|
VERSION = '0.10.1'
|
||||||
|
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
|
|
|
@ -7,6 +7,9 @@ that were made in every particular version.
|
||||||
From version 0.7.6 *Dependency Injector* framework strictly
|
From version 0.7.6 *Dependency Injector* framework strictly
|
||||||
follows `Semantic versioning`_
|
follows `Semantic versioning`_
|
||||||
|
|
||||||
|
0.10.1
|
||||||
|
------
|
||||||
|
- Update some examples.
|
||||||
|
|
||||||
0.10.0
|
0.10.0
|
||||||
------
|
------
|
||||||
|
|
|
@ -4,56 +4,60 @@ import sqlite3
|
||||||
import dependency_injector as di
|
import dependency_injector as di
|
||||||
|
|
||||||
|
|
||||||
class ObjectA(object):
|
class UsersService(object):
|
||||||
"""Example class ObjectA, that has dependency on database."""
|
"""Users service, that has dependency on database."""
|
||||||
|
|
||||||
def __init__(self, db):
|
def __init__(self, db):
|
||||||
"""Initializer."""
|
"""Initializer."""
|
||||||
self.db = db
|
self.db = db
|
||||||
|
|
||||||
|
|
||||||
class ObjectB(object):
|
class AuthService(object):
|
||||||
"""Example class ObjectB, that has dependencies on ObjectA and database."""
|
"""Auth service, that has dependencies on users service and database."""
|
||||||
|
|
||||||
def __init__(self, a, db):
|
def __init__(self, db, users_service):
|
||||||
"""Initializer."""
|
"""Initializer."""
|
||||||
self.a = a
|
|
||||||
self.db = db
|
self.db = db
|
||||||
|
self.users_service = users_service
|
||||||
|
|
||||||
|
|
||||||
class Catalog(di.AbstractCatalog):
|
class Services(di.AbstractCatalog):
|
||||||
"""Catalog of providers."""
|
"""Catalog of service providers."""
|
||||||
|
|
||||||
database = di.Singleton(sqlite3.Connection,
|
database = di.Singleton(sqlite3.connect, ':memory:')
|
||||||
database=':memory:')
|
""":type: () -> sqlite3.Connection"""
|
||||||
""":type: (di.Provider) -> sqlite3.Connection"""
|
|
||||||
|
|
||||||
object_a_factory = di.Factory(ObjectA,
|
users = di.Factory(UsersService,
|
||||||
db=database)
|
db=database)
|
||||||
""":type: (di.Provider) -> ObjectA"""
|
""":type: () -> UsersService"""
|
||||||
|
|
||||||
object_b_factory = di.Factory(ObjectB,
|
auth = di.Factory(AuthService,
|
||||||
a=object_a_factory,
|
db=database,
|
||||||
db=database)
|
users_service=users)
|
||||||
""":type: (di.Provider) -> ObjectB"""
|
""":type: () -> AuthService"""
|
||||||
|
|
||||||
|
|
||||||
# Catalog static provides.
|
# Retrieving catalog providers:
|
||||||
a1, a2 = Catalog.object_a_factory(), Catalog.object_a_factory()
|
users_service = Services.users()
|
||||||
b1, b2 = Catalog.object_b_factory(), Catalog.object_b_factory()
|
auth_service = Services.auth()
|
||||||
|
|
||||||
assert a1 is not a2
|
# Making some asserts:
|
||||||
assert b1 is not b2
|
assert users_service.db is auth_service.db is Services.database()
|
||||||
assert a1.db is a2.db is b1.db is b2.db is Catalog.database()
|
assert isinstance(auth_service.users_service, UsersService)
|
||||||
|
assert users_service is not Services.users()
|
||||||
|
assert auth_service is not Services.auth()
|
||||||
|
|
||||||
|
|
||||||
# Example of inline injections.
|
# Making some "inline" injections:
|
||||||
@di.inject(a=Catalog.object_a_factory)
|
@di.inject(users_service=Services.users)
|
||||||
@di.inject(b=Catalog.object_b_factory)
|
@di.inject(auth_service=Services.auth)
|
||||||
@di.inject(database=Catalog.database)
|
@di.inject(database=Services.database)
|
||||||
def example(a, b, database):
|
def example(users_service, auth_service, database):
|
||||||
"""Example callback."""
|
"""Example callback."""
|
||||||
assert a.db is b.db is database is Catalog.database()
|
assert users_service.db is auth_service.db
|
||||||
|
assert auth_service.db is database
|
||||||
|
assert database is Services.database()
|
||||||
|
|
||||||
|
|
||||||
|
# Making a call of decorated callback:
|
||||||
example()
|
example()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user