mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Edited comments & titles
This commit is contained in:
parent
720d192258
commit
ce989cbee0
2 changed files with 8 additions and 8 deletions
|
|
@ -1,12 +1,12 @@
|
|||
######
|
||||
####################
|
||||
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.
|
||||
|
||||
|
||||
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:
|
||||
|
|
@ -20,7 +20,7 @@ To access contextual data you should specify corresponding keyword parameter in
|
|||
|
||||
|
||||
Injecting own dependencies
|
||||
======
|
||||
==========================
|
||||
|
||||
Aiogram provides several ways to complement / modify contextual data.
|
||||
|
||||
|
|
|
|||
|
|
@ -16,17 +16,17 @@ class HelloFilter(Filter):
|
|||
self,
|
||||
message: Message,
|
||||
event_from_user: User
|
||||
# as I said previously, filters also can accept keyword parameters like in handlers
|
||||
# Filters also can accept keyword parameters like in handlers
|
||||
) -> Union[bool, Dict[str, Any]]:
|
||||
if message.text.casefold() == "hello":
|
||||
# returning a dictionary that will update the context data
|
||||
# Returning a dictionary that will update the context data
|
||||
return {"name": event_from_user.mention_html(name=self.name)}
|
||||
return False
|
||||
|
||||
|
||||
@router.message(HelloFilter(name=None))
|
||||
@router.message(HelloFilter())
|
||||
async def my_handler(
|
||||
message: Message, name: str # and now we can accept "name" as named parameter
|
||||
message: Message, name: str # Now we can accept "name" as named parameter
|
||||
) -> Any:
|
||||
return message.answer(
|
||||
"Hello, {name}!".format(name=name)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue