Update provider overriding docs

This commit is contained in:
Roman Mogilatov 2015-11-23 17:02:48 +02:00
parent 91dcfea775
commit 9b4e325a2d
6 changed files with 16 additions and 15 deletions

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

View File

@ -1,6 +1,8 @@
Overriding of providers Overriding of providers
----------------------- -----------------------
.. module:: dependency_injector.providers
Every provider could be overridden by another provider. Every provider could be overridden by another provider.
This gives opportunity to make system behaviour more flexible in some points. This gives opportunity to make system behaviour more flexible in some points.
@ -15,22 +17,22 @@ Provider overriding functionality has such interface:
:width: 45% :width: 45%
:align: center :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. instead of current provider. This method could be called several times.
In such case, last passed provider would be used as overriding one. In such case, last passed provider would be used as overriding one.
+ ``di.Provider.reset_override()`` - resets all overriding providers. Provider + :py:meth:`Provider.reset_override()` - resets all overriding providers.
starts to behave itself like usual. Provider starts to behave itself like usual.
+ ``di.Provider.is_overridden`` - bool, ``True`` if provider is overridden. + :py:attr:`Provider.is_overridden` - bool, ``True`` if provider is overridden.
.. note:: .. note::
Actually, initial provider forms stack from overriding providers. There is Actually, initial provider forms stack from overriding providers. There is
some, not so common, but still usefull, functionality that could be used: 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. overriding provider.
+ ``di.Provider.reset_last_overriding()`` - remove last overriding provider + :py:meth:`Provider.reset_last_overriding()` - remove last overriding
from stack of overriding providers. provider from stack of overriding providers.
Example: Example:

View File

@ -1,13 +1,13 @@
"""Simple providers overriding example.""" """Simple providers overriding example."""
import dependency_injector as di from dependency_injector import providers
class User(object): class User(object):
"""Example class User.""" """Example class User."""
# Users factory: # Users factory:
users_factory = di.Factory(User) users_factory = providers.Factory(User)
# Creating several User objects: # Creating several User objects:
user1 = users_factory() user1 = users_factory()
@ -23,7 +23,7 @@ class SuperUser(User):
"""Example class SuperUser.""" """Example class SuperUser."""
# Overriding users factory: # Overriding users factory:
users_factory.override(di.Factory(SuperUser)) users_factory.override(providers.Factory(SuperUser))
# Creating some more User objects using overridden users factory: # Creating some more User objects using overridden users factory:
user3 = users_factory() user3 = users_factory()

View File

@ -1,6 +1,6 @@
"""Overriding user's model example.""" """Overriding user's model example."""
import dependency_injector as di from dependency_injector import providers
class User(object): class User(object):
@ -26,8 +26,7 @@ class UserService(object):
return self.user_cls(id=id, password='secret' + str(id)) return self.user_cls(id=id, password='secret' + str(id))
# Users factory and UserService provider: # Users factory and UserService provider:
users_service = di.Factory(UserService, users_service = providers.Factory(UserService, user_cls=User)
user_cls=User)
# Getting several users and making some asserts: # Getting several users and making some asserts:
user1 = users_service().get_by_id(1) user1 = users_service().get_by_id(1)
@ -71,8 +70,8 @@ class ExtendedUserService(UserService):
return user return user
# Overriding users_service provider: # Overriding users_service provider:
extended_users_service = di.Factory(ExtendedUserService, extended_users_service = providers.Factory(ExtendedUserService,
user_cls=ExtendedUser) user_cls=ExtendedUser)
users_service.override(extended_users_service) users_service.override(extended_users_service)
# Getting few other users users and making some asserts: # Getting few other users users and making some asserts: