Edited comments & titles

This commit is contained in:
nullmatawasoradesu 2023-08-10 23:43:31 +03:00
parent 720d192258
commit ce989cbee0
2 changed files with 8 additions and 8 deletions

View file

@ -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.

View file

@ -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)