mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Update provider overriding docs
This commit is contained in:
parent
91dcfea775
commit
9b4e325a2d
Binary file not shown.
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 36 KiB |
Binary file not shown.
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 78 KiB |
Binary file not shown.
Before Width: | Height: | Size: 9.4 KiB After Width: | Height: | Size: 25 KiB |
|
@ -1,6 +1,8 @@
|
|||
Overriding of providers
|
||||
-----------------------
|
||||
|
||||
.. module:: dependency_injector.providers
|
||||
|
||||
Every provider could be overridden by another provider.
|
||||
|
||||
This gives opportunity to make system behaviour more flexible in some points.
|
||||
|
@ -15,22 +17,22 @@ Provider overriding functionality has such interface:
|
|||
:width: 45%
|
||||
:align: center
|
||||
|
||||
+ ``di.Provider.override()`` - takes another provider that will be used
|
||||
+ :py:meth:`Provider.override()` - takes another provider that will be used
|
||||
instead of current provider. This method could be called several times.
|
||||
In such case, last passed provider would be used as overriding one.
|
||||
+ ``di.Provider.reset_override()`` - resets all overriding providers. Provider
|
||||
starts to behave itself like usual.
|
||||
+ ``di.Provider.is_overridden`` - bool, ``True`` if provider is overridden.
|
||||
+ :py:meth:`Provider.reset_override()` - resets all overriding providers.
|
||||
Provider starts to behave itself like usual.
|
||||
+ :py:attr:`Provider.is_overridden` - bool, ``True`` if provider is overridden.
|
||||
|
||||
.. note::
|
||||
|
||||
Actually, initial provider forms stack from overriding providers. There is
|
||||
some, not so common, but still usefull, functionality that could be used:
|
||||
|
||||
+ ``di.Provider.last_overriding`` - always keeps reference to last
|
||||
+ :py:attr:`Provider.last_overriding` - always keeps reference to last
|
||||
overriding provider.
|
||||
+ ``di.Provider.reset_last_overriding()`` - remove last overriding provider
|
||||
from stack of overriding providers.
|
||||
+ :py:meth:`Provider.reset_last_overriding()` - remove last overriding
|
||||
provider from stack of overriding providers.
|
||||
|
||||
Example:
|
||||
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
"""Simple providers overriding example."""
|
||||
|
||||
import dependency_injector as di
|
||||
from dependency_injector import providers
|
||||
|
||||
|
||||
class User(object):
|
||||
"""Example class User."""
|
||||
|
||||
# Users factory:
|
||||
users_factory = di.Factory(User)
|
||||
users_factory = providers.Factory(User)
|
||||
|
||||
# Creating several User objects:
|
||||
user1 = users_factory()
|
||||
|
@ -23,7 +23,7 @@ class SuperUser(User):
|
|||
"""Example class SuperUser."""
|
||||
|
||||
# Overriding users factory:
|
||||
users_factory.override(di.Factory(SuperUser))
|
||||
users_factory.override(providers.Factory(SuperUser))
|
||||
|
||||
# Creating some more User objects using overridden users factory:
|
||||
user3 = users_factory()
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""Overriding user's model example."""
|
||||
|
||||
import dependency_injector as di
|
||||
from dependency_injector import providers
|
||||
|
||||
|
||||
class User(object):
|
||||
|
@ -26,8 +26,7 @@ class UserService(object):
|
|||
return self.user_cls(id=id, password='secret' + str(id))
|
||||
|
||||
# Users factory and UserService provider:
|
||||
users_service = di.Factory(UserService,
|
||||
user_cls=User)
|
||||
users_service = providers.Factory(UserService, user_cls=User)
|
||||
|
||||
# Getting several users and making some asserts:
|
||||
user1 = users_service().get_by_id(1)
|
||||
|
@ -71,8 +70,8 @@ class ExtendedUserService(UserService):
|
|||
return user
|
||||
|
||||
# Overriding users_service provider:
|
||||
extended_users_service = di.Factory(ExtendedUserService,
|
||||
user_cls=ExtendedUser)
|
||||
extended_users_service = providers.Factory(ExtendedUserService,
|
||||
user_cls=ExtendedUser)
|
||||
users_service.override(extended_users_service)
|
||||
|
||||
# Getting few other users users and making some asserts:
|
||||
|
|
Loading…
Reference in New Issue
Block a user