mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Update example of writing custom providers
This commit is contained in:
parent
e8bd524b6d
commit
d8d910d342
|
@ -16,6 +16,7 @@ Development version
|
|||
- Add ``Provider.last_overriding`` read-only property that points to last
|
||||
overriding provider, if any. If target provider is not overridden, ``None``
|
||||
would be returned.
|
||||
- Update example of writing custom providers.
|
||||
|
||||
3.4.3
|
||||
-----
|
||||
|
|
|
@ -12,9 +12,7 @@ Below are some tips and recommendations that have to be met:
|
|||
2. Cusom provider's ``__init__()`` could be overriden, but parent's
|
||||
initializer (:py:meth:`Provider.__init__`) has to be called.
|
||||
3. Providing strategy has to be implemented in custom provider's
|
||||
:py:meth:`Provider._provide` method. All ``*args`` & ``**kwargs``
|
||||
that will be recieved by :py:meth:`Provider.__call__` will be
|
||||
transefed to custom provider's :py:meth:`Provider._provide`.
|
||||
:py:meth:`Provider.__call__` method.
|
||||
4. If custom provider is based on some standard providers, it is better to
|
||||
use delegation of standard providers, then extending of them.
|
||||
5. If custom provider defines any attributes, it is good to list them in
|
||||
|
|
|
@ -17,17 +17,13 @@ class UsersFactory(providers.Provider):
|
|||
self._factory = providers.Factory(User)
|
||||
super(UsersFactory, self).__init__()
|
||||
|
||||
def _provide(self, *args, **kwargs):
|
||||
"""Return provided instance.
|
||||
def __call__(self, *args, **kwargs):
|
||||
"""Return provided object.
|
||||
|
||||
:param args: tuple of context positional arguments
|
||||
:type args: tuple[object]
|
||||
|
||||
:param kwargs: dictionary of context keyword arguments
|
||||
:type kwargs: dict[str, object]
|
||||
|
||||
:rtype: object
|
||||
Callable interface implementation.
|
||||
"""
|
||||
if self.last_overriding is not None:
|
||||
return self.last_overriding._provide(args, kwargs)
|
||||
return self._factory(*args, **kwargs)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user