diff --git a/docs/images/providers/overriding_simple.png b/docs/images/providers/overriding_simple.png index a3d949da..5efb1e78 100644 Binary files a/docs/images/providers/overriding_simple.png and b/docs/images/providers/overriding_simple.png differ diff --git a/docs/images/providers/overriding_users_model.png b/docs/images/providers/overriding_users_model.png index f2763621..86c534b2 100644 Binary files a/docs/images/providers/overriding_users_model.png and b/docs/images/providers/overriding_users_model.png differ diff --git a/docs/images/providers/provider_override.png b/docs/images/providers/provider_override.png index 2a523605..19a5057b 100644 Binary files a/docs/images/providers/provider_override.png and b/docs/images/providers/provider_override.png differ diff --git a/docs/providers/overriding.rst b/docs/providers/overriding.rst index 17c887d3..6848641c 100644 --- a/docs/providers/overriding.rst +++ b/docs/providers/overriding.rst @@ -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: diff --git a/examples/providers/overriding_simple.py b/examples/providers/overriding_simple.py index f99d0b64..4d99cf31 100644 --- a/examples/providers/overriding_simple.py +++ b/examples/providers/overriding_simple.py @@ -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() diff --git a/examples/providers/overriding_users_model.py b/examples/providers/overriding_users_model.py index e3db9be2..6fdc7fed 100644 --- a/examples/providers/overriding_users_model.py +++ b/examples/providers/overriding_users_model.py @@ -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: