mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-26 11:33:58 +03:00
Make some changes in introduction docs
This commit is contained in:
parent
0df57272fd
commit
c8115ed44e
|
@ -1,6 +1,9 @@
|
||||||
Dependency injection and inversion of control in Python
|
Dependency injection and inversion of control in Python
|
||||||
-------------------------------------------------------
|
-------------------------------------------------------
|
||||||
|
|
||||||
|
History
|
||||||
|
~~~~~~~
|
||||||
|
|
||||||
Originally, dependency injection pattern got popular in languages with static
|
Originally, dependency injection pattern got popular in languages with static
|
||||||
typing, like Java. Dependency injection framework can
|
typing, like Java. Dependency injection framework can
|
||||||
significantly improve flexibility of the language with static typing. Also,
|
significantly improve flexibility of the language with static typing. Also,
|
||||||
|
@ -14,6 +17,9 @@ for Java. Also there is a meaning that dependency injection framework is
|
||||||
something that Python developer would not ever need, cause dependency injection
|
something that Python developer would not ever need, cause dependency injection
|
||||||
could be implemented easily using language fundamentals.
|
could be implemented easily using language fundamentals.
|
||||||
|
|
||||||
|
Discussion
|
||||||
|
~~~~~~~~~~
|
||||||
|
|
||||||
It is true.
|
It is true.
|
||||||
|
|
||||||
Partly.
|
Partly.
|
||||||
|
@ -75,6 +81,9 @@ Main design purposes of using inversion of control are:
|
||||||
instead rely on contracts.
|
instead rely on contracts.
|
||||||
+ To prevent side effects when replacing a module.
|
+ To prevent side effects when replacing a module.
|
||||||
|
|
||||||
|
Example
|
||||||
|
~~~~~~~
|
||||||
|
|
||||||
Let's go through next example:
|
Let's go through next example:
|
||||||
|
|
||||||
.. literalinclude:: ../../../examples/ioc_demo/car_engine_1.py
|
.. literalinclude:: ../../../examples/ioc_demo/car_engine_1.py
|
||||||
|
|
|
@ -8,35 +8,37 @@ interaction between each other.
|
||||||
:width: 100%
|
:width: 100%
|
||||||
:align: center
|
:align: center
|
||||||
|
|
||||||
There are 3 main entities:
|
There are 3 main entities: providers, injections and catalogs.
|
||||||
|
|
||||||
.. glossary::
|
|
||||||
|
|
||||||
Providers
|
|
||||||
Providers are strategies of accesing objects. For example,
|
|
||||||
:py:class:`dependency_injector.providers.Factory` creates new instance
|
|
||||||
of provided class every time it is called.
|
|
||||||
:py:class:`dependency_injector.providers.Singleton` creates provided
|
|
||||||
instance once and returns it on every next call. Providers could be
|
|
||||||
injected into each other. Providers could be overridden by another
|
|
||||||
providers. Base class is -
|
|
||||||
:py:class:`dependency_injector.providers.Provider`.
|
|
||||||
|
|
||||||
Injections
|
|
||||||
Injections are instructions for making dependency injections
|
|
||||||
(there are several ways how they could be done). Injections are used
|
|
||||||
mostly by :py:class:`dependency_injector.providers.Factory` and
|
|
||||||
:py:class:`dependency_injector.providers.Singleton` providers, but
|
|
||||||
these are not only cases. Base class is -
|
|
||||||
:py:class:`dependency_injector.injections.Injection`.
|
|
||||||
|
|
||||||
Catalogs
|
|
||||||
Catalogs are collections of providers. They are used for grouping
|
|
||||||
of providers by some principles. Base class is -
|
|
||||||
:py:class:`dependency_injector.catalogs.DeclarativeCatalog`.
|
|
||||||
|
|
||||||
Some general principles about *Dependency Injector* entities:
|
|
||||||
|
|
||||||
+ All of the entities could be used in composition with each other or
|
+ All of the entities could be used in composition with each other or
|
||||||
separatelly.
|
separatelly.
|
||||||
+ Each of the entities could be extended via specialization.
|
+ Each of the entities could be extended via specialization.
|
||||||
|
|
||||||
|
Providers
|
||||||
|
~~~~~~~~~
|
||||||
|
|
||||||
|
Providers are strategies of accesing objects. For example,
|
||||||
|
:py:class:`dependency_injector.providers.Factory` creates new instance
|
||||||
|
of provided class every time it is called.
|
||||||
|
:py:class:`dependency_injector.providers.Singleton` creates provided
|
||||||
|
instance once and returns it on every next call. Providers could be
|
||||||
|
injected into each other. Providers could be overridden by another
|
||||||
|
providers. Base class is -
|
||||||
|
:py:class:`dependency_injector.providers.Provider`.
|
||||||
|
|
||||||
|
Injections
|
||||||
|
~~~~~~~~~~
|
||||||
|
|
||||||
|
Injections are instructions for making dependency injections
|
||||||
|
(there are several ways how they could be done). Injections are used
|
||||||
|
mostly by :py:class:`dependency_injector.providers.Factory` and
|
||||||
|
:py:class:`dependency_injector.providers.Singleton` providers, but
|
||||||
|
these are not only cases. Base class is -
|
||||||
|
:py:class:`dependency_injector.injections.Injection`.
|
||||||
|
|
||||||
|
Catalogs
|
||||||
|
~~~~~~~~~
|
||||||
|
|
||||||
|
Catalogs are collections of providers. They are used for grouping
|
||||||
|
of providers by some principles. Base class is -
|
||||||
|
:py:class:`dependency_injector.catalogs.DeclarativeCatalog`.
|
||||||
|
|
|
@ -8,9 +8,7 @@ class Components(catalogs.DeclarativeCatalog):
|
||||||
"""Components catalog."""
|
"""Components catalog."""
|
||||||
|
|
||||||
service = providers.Factory(Service)
|
service = providers.Factory(Service)
|
||||||
|
client = providers.Factory(Client, service=service)
|
||||||
client = providers.Factory(Client,
|
|
||||||
service=service)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in New Issue
Block a user