mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 11:04:01 +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):
|
||||
"""Catalog of service providers."""
|
||||
|
||||
database = providers.Singleton(sqlite3.connect,
|
||||
injections.Arg(':memory:'))
|
||||
database = providers.Singleton(sqlite3.connect, ':memory:')
|
||||
""":type: providers.Provider -> sqlite3.Connection"""
|
||||
|
||||
users = providers.Factory(UsersService,
|
||||
injections.KwArg('db', database))
|
||||
db=database)
|
||||
""":type: providers.Provider -> UsersService"""
|
||||
|
||||
auth = providers.Factory(AuthService,
|
||||
injections.KwArg('db', database),
|
||||
injections.KwArg('users_service', users))
|
||||
db=database,
|
||||
users_service=users)
|
||||
""":type: providers.Provider -> AuthService"""
|
||||
|
||||
|
||||
|
@ -132,6 +131,28 @@ Examples
|
|||
# Making a call of decorated callback:
|
||||
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
|
||||
GitHub:
|
||||
|
||||
|
|
|
@ -64,3 +64,25 @@ def example(users_service, auth_service, database):
|
|||
|
||||
# Making a call of decorated callback:
|
||||
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:
|
||||
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