mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-06 07:50:32 +00:00
Fixed structure of DI docs page, included it in toctree
This commit is contained in:
parent
4d12e073ee
commit
ac124f5b08
2 changed files with 26 additions and 9 deletions
|
|
@ -2,16 +2,23 @@
|
|||
Dependency injection
|
||||
####################
|
||||
|
||||
Dependency injection is a programming technique that makes a class independent of its dependencies. It achieves that by decoupling the usage of an object from its creation. This helps you to follow `SOLID's <https://en.wikipedia.org/wiki/SOLID>`_ dependency inversion and single responsibility principles.
|
||||
Dependency injection is a programming technique that makes a class independent of its dependencies.
|
||||
It achieves that by decoupling the usage of an object from its creation.
|
||||
This helps you to follow `SOLID's <https://en.wikipedia.org/wiki/SOLID>`_ dependency
|
||||
inversion and single responsibility principles.
|
||||
|
||||
|
||||
How it works in aiogram
|
||||
=======================
|
||||
For each update :class:`Dispatcher` passes handling context data. Filters and middleware can also make changes to the context.
|
||||
|
||||
To access contextual data you should specify corresponding keyword parameter in handler or filter. For example, to get :class:`FSMContext` we do it like that:
|
||||
For each update :class:`aiogram.dispatcher.dispatcher.Dispatcher` passes handling context data.
|
||||
Filters and middleware can also make changes to the context.
|
||||
|
||||
To access contextual data you should specify corresponding keyword parameter in handler or filter.
|
||||
For example, to get :class:`aiogram.fsm.context.FSMContext` we do it like that:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@router.message(ProfileCompletion.add_photo, F.photo)
|
||||
async def add_photo(
|
||||
message: types.Message, bot: Bot, state: FSMContext
|
||||
|
|
@ -24,33 +31,42 @@ Injecting own dependencies
|
|||
|
||||
Aiogram provides several ways to complement / modify contextual data.
|
||||
|
||||
The first and easiest way is to simply specify the named arguments in :class:`Dispatcher` initialization, polling start methods or :class:`SimpleRequestHandler` initialization if you use webhooks.
|
||||
The first and easiest way is to simply specify the named arguments in
|
||||
:class:`aiogram.dispatcher.dispatcher.Dispatcher` initialization, polling start methods
|
||||
or :class:`aiogram.webhook.aiohttp_server.SimpleRequestHandler` initialization if you use webhooks.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
async def main() -> None:
|
||||
dp = Dispatcher(..., foo=42)
|
||||
return await dp.start_polling(
|
||||
bot, allowed_updates=dp.resolve_used_update_types(), bar="Bazz"
|
||||
bot, bar="Bazz"
|
||||
)
|
||||
|
||||
Analogy for webhook:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
async def main() -> None:
|
||||
dp = Dispatcher(..., foo=42)
|
||||
handler = SimpleRequestHandler(dispatcher=dp, bot=bot, bar="Bazz")
|
||||
... # starting webhook
|
||||
:class:`Dispatcher`'s workflow data also can be supplemented by setting values as in a dictionary:
|
||||
|
||||
:class:`aiogram.dispatcher.dispatcher.Dispatcher`'s workflow data also can be supplemented
|
||||
by setting values as in a dictionary:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
dp = Dispatcher(...)
|
||||
dp["eggs"] = Spam()
|
||||
|
||||
The middlewares updates the context quite often.
|
||||
You can read more about them on this page:
|
||||
|
||||
- `Middlewares <middlewares.html>`__
|
||||
- :ref:`Middlewares <middlewares>`
|
||||
|
||||
The last way is to return a dictionary from the filter:
|
||||
|
||||
.. literalinclude:: ../../../examples/context_addition_from_filter.py
|
||||
.. literalinclude:: ../../examples/context_addition_from_filter.py
|
||||
|
||||
...or using MagicFilter with :code:`as_()` method. (`Read more <filters/magic_filters.html#get-filter-result-as-handler-argument>`__)
|
||||
...or using :ref:`MagicFilter <magic-filters>` with :code:`.as_(...)` method.
|
||||
|
|
|
|||
|
|
@ -34,3 +34,4 @@ So, you can use both of them with *aiogram*.
|
|||
errors
|
||||
long_polling
|
||||
webhook
|
||||
dependency_injection
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue