mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 17:47:02 +03:00
Update concept examples with provider overriding example
This commit is contained in:
parent
0031003a14
commit
f1429d557a
31
README.rst
31
README.rst
|
@ -93,17 +93,16 @@ Examples
|
||||||
class Services(catalogs.DeclarativeCatalog):
|
class Services(catalogs.DeclarativeCatalog):
|
||||||
"""Catalog of service providers."""
|
"""Catalog of service providers."""
|
||||||
|
|
||||||
database = providers.Singleton(sqlite3.connect,
|
database = providers.Singleton(sqlite3.connect, ':memory:')
|
||||||
injections.Arg(':memory:'))
|
|
||||||
""":type: providers.Provider -> sqlite3.Connection"""
|
""":type: providers.Provider -> sqlite3.Connection"""
|
||||||
|
|
||||||
users = providers.Factory(UsersService,
|
users = providers.Factory(UsersService,
|
||||||
injections.KwArg('db', database))
|
db=database)
|
||||||
""":type: providers.Provider -> UsersService"""
|
""":type: providers.Provider -> UsersService"""
|
||||||
|
|
||||||
auth = providers.Factory(AuthService,
|
auth = providers.Factory(AuthService,
|
||||||
injections.KwArg('db', database),
|
db=database,
|
||||||
injections.KwArg('users_service', users))
|
users_service=users)
|
||||||
""":type: providers.Provider -> AuthService"""
|
""":type: providers.Provider -> AuthService"""
|
||||||
|
|
||||||
|
|
||||||
|
@ -132,6 +131,28 @@ Examples
|
||||||
# Making a call of decorated callback:
|
# Making a call of decorated callback:
|
||||||
example()
|
example()
|
||||||
|
|
||||||
|
|
||||||
|
# Overriding auth service provider and making some asserts:
|
||||||
|
class ExtendedAuthService(AuthService):
|
||||||
|
"""Extended version of auth service."""
|
||||||
|
|
||||||
|
def __init__(self, db, users_service, ttl):
|
||||||
|
"""Initializer."""
|
||||||
|
self.ttl = ttl
|
||||||
|
super(ExtendedAuthService, self).__init__(db=db,
|
||||||
|
users_service=users_service)
|
||||||
|
|
||||||
|
|
||||||
|
Services.auth.override(providers.Factory(ExtendedAuthService,
|
||||||
|
db=Services.database,
|
||||||
|
users_service=Services.users,
|
||||||
|
ttl=3600))
|
||||||
|
|
||||||
|
|
||||||
|
auth_service = Services.auth()
|
||||||
|
|
||||||
|
assert isinstance(auth_service, ExtendedAuthService)
|
||||||
|
|
||||||
You can get more *Dependency Injector* examples in ``/examples`` directory on
|
You can get more *Dependency Injector* examples in ``/examples`` directory on
|
||||||
GitHub:
|
GitHub:
|
||||||
|
|
||||||
|
|
|
@ -64,3 +64,25 @@ def example(users_service, auth_service, database):
|
||||||
|
|
||||||
# Making a call of decorated callback:
|
# Making a call of decorated callback:
|
||||||
example()
|
example()
|
||||||
|
|
||||||
|
|
||||||
|
# Overriding auth service provider and making some asserts:
|
||||||
|
class ExtendedAuthService(AuthService):
|
||||||
|
"""Extended version of auth service."""
|
||||||
|
|
||||||
|
def __init__(self, db, users_service, ttl):
|
||||||
|
"""Initializer."""
|
||||||
|
self.ttl = ttl
|
||||||
|
super(ExtendedAuthService, self).__init__(db=db,
|
||||||
|
users_service=users_service)
|
||||||
|
|
||||||
|
|
||||||
|
Services.auth.override(providers.Factory(ExtendedAuthService,
|
||||||
|
db=Services.database,
|
||||||
|
users_service=Services.users,
|
||||||
|
ttl=3600))
|
||||||
|
|
||||||
|
|
||||||
|
auth_service = Services.auth()
|
||||||
|
|
||||||
|
assert isinstance(auth_service, ExtendedAuthService)
|
||||||
|
|
|
@ -65,3 +65,27 @@ def example(users_service, auth_service, database):
|
||||||
|
|
||||||
# Making a call of decorated callback:
|
# Making a call of decorated callback:
|
||||||
example()
|
example()
|
||||||
|
|
||||||
|
|
||||||
|
# Overriding auth service provider and making some asserts:
|
||||||
|
class ExtendedAuthService(AuthService):
|
||||||
|
"""Extended version of auth service."""
|
||||||
|
|
||||||
|
def __init__(self, db, users_service, ttl):
|
||||||
|
"""Initializer."""
|
||||||
|
self.ttl = ttl
|
||||||
|
super(ExtendedAuthService, self).__init__(db=db,
|
||||||
|
users_service=users_service)
|
||||||
|
|
||||||
|
|
||||||
|
Services.auth.override(providers.Factory(ExtendedAuthService,
|
||||||
|
injections.KwArg('db',
|
||||||
|
Services.database),
|
||||||
|
injections.KwArg('users_service',
|
||||||
|
Services.users),
|
||||||
|
injections.KwArg('ttl', 3600)))
|
||||||
|
|
||||||
|
|
||||||
|
auth_service = Services.auth()
|
||||||
|
|
||||||
|
assert isinstance(auth_service, ExtendedAuthService)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user