1- Injecting attributes, items, or call methods of the provided instance
2- =====================================================================
1+ Injecting provided object attributes, items, or call its methods
2+ ================================================================
33
4- .. currentmodule :: dependency_injector.providers
4+ .. meta ::
5+ :keywords: Python,DI,Dependency injection,IoC,Inversion of Control,Attribute,Method,Call
6+ :description: This page demonstrates how to inject attributes, items or call method of the
7+ provided instance.
58
6- In this section you will know how to inject provided instance attribute or item into the other
7- provider.
9+ .. currentmodule :: dependency_injector.providers
810
9- It also describes how to call a method of the provided instance and use the result of
10- this call as an injection value.
11+ You can inject provided object attribute, item or result of its method call.
1112
1213.. literalinclude :: ../../examples/providers/provided_instance.py
1314 :language: python
1415 :emphasize-lines: 26-32
1516 :lines: 3-
1617
1718To use the feature you should use the ``.provided `` attribute of the injected provider. This
18- attribute helps to specify what happens with the provided instance. You can retrieve an injection
19- value from :
19+ attribute helps to specify what happens with the provided instance before the injection. You can
20+ use any combination of the following :
2021
21- - an attribute of the provided instance
22- - an item of the provided instance
23- - a call of the provided instance method
22+ - an attribute of the provided object
23+ - an item of the provided object
24+ - a call of the provided object method
2425
25- When you use the call of the provided instance method you can specify the injections into this
26+ When you use a call of the provided instance method you can specify the injections for this
2627method like you do with any other provider.
2728
2829You can do nested constructions:
@@ -32,35 +33,24 @@ You can do nested constructions:
3233 :emphasize-lines: 24-30
3334 :lines: 3-
3435
35- Attribute ``.provided `` is available for the providers that return instances. Providers that
36- have ``.provided `` attribute:
36+ The ``.provided `` attribute is available for the next providers:
3737
38- - :py:class: `Callable ` and its subclasses
3938- :py:class: `Factory ` and its subclasses
4039- :py:class: `Singleton ` and its subclasses
40+ - :py:class: `Callable ` and its subclasses
4141- :py:class: `Object `
4242- :py:class: `List `
4343- :py:class: `Selector `
4444- :py:class: `Dependency `
4545
46- Special providers like :py:class: `Configuration ` or :py:class: `Delegate ` do not have the
47- ``.provided `` attribute.
48-
49- Provider subclasses
50- -------------------
51-
5246When you create a new provider subclass and want to implement the ``.provided `` attribute, you
53- should use the :py:class: `ProvidedInstance ` provider.
47+ should use the :py:class: `ProvidedInstance ` provider. Add the ``.provided `` property
48+ implementation to a new subclass:
5449
5550.. code-block :: python
5651
5752 @ property
5853 def provided (self ):
59- """ Return :py:class:`ProvidedInstance` provider."""
6054 return ProvidedInstance(self )
6155
62- In all other cases you should not use :py:class: `ProvidedInstance `, :py:class: `AttributeGetter `,
63- :py:class: `ItemGetter `, or :py:class: `MethodCaller ` providers directly. Use the ``.provided ``
64- attribute of the injected provider instead.
65-
6656 .. disqus ::
0 commit comments