2020-09-03 01:15:06 +00:00
|
|
|
.. _provided-instance:
|
|
|
|
|
2020-09-02 21:59:31 +00:00
|
|
|
Injecting provided object attributes, items, or call its methods
|
|
|
|
================================================================
|
2020-08-21 01:52:12 +00:00
|
|
|
|
2020-09-02 21:59:31 +00:00
|
|
|
.. meta::
|
|
|
|
:keywords: Python,DI,Dependency injection,IoC,Inversion of Control,Attribute,Method,Call
|
|
|
|
:description: This page demonstrates how to inject attributes, items or call method of the
|
|
|
|
provided instance.
|
2020-08-21 01:52:12 +00:00
|
|
|
|
2020-09-02 21:59:31 +00:00
|
|
|
.. currentmodule:: dependency_injector.providers
|
2020-08-21 01:52:12 +00:00
|
|
|
|
2020-09-02 21:59:31 +00:00
|
|
|
You can inject provided object attribute, item or result of its method call.
|
2020-08-21 01:52:12 +00:00
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/providers/provided_instance.py
|
|
|
|
:language: python
|
2020-09-03 21:48:45 +00:00
|
|
|
:emphasize-lines: 28-34
|
2020-08-21 01:52:12 +00:00
|
|
|
:lines: 3-
|
|
|
|
|
|
|
|
To use the feature you should use the ``.provided`` attribute of the injected provider. This
|
2020-09-02 21:59:31 +00:00
|
|
|
attribute helps to specify what happens with the provided instance before the injection. You can
|
|
|
|
use any combination of the following:
|
2020-08-21 01:52:12 +00:00
|
|
|
|
2020-09-02 21:59:31 +00:00
|
|
|
- an attribute of the provided object
|
|
|
|
- an item of the provided object
|
|
|
|
- a call of the provided object method
|
2020-08-21 01:52:12 +00:00
|
|
|
|
2020-09-02 21:59:31 +00:00
|
|
|
When you use a call of the provided instance method you can specify the injections for this
|
2020-08-21 01:52:12 +00:00
|
|
|
method like you do with any other provider.
|
|
|
|
|
|
|
|
You can do nested constructions:
|
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/providers/provided_instance_complex.py
|
|
|
|
:language: python
|
2020-09-03 21:48:45 +00:00
|
|
|
:emphasize-lines: 26-32
|
2020-08-21 01:52:12 +00:00
|
|
|
:lines: 3-
|
|
|
|
|
2020-08-24 17:34:47 +00:00
|
|
|
.. disqus::
|