diff --git a/docs/dispatcher/dependency_injection.rst b/docs/dispatcher/dependency_injection.rst index 5f0e4b2f..15b7c908 100644 --- a/docs/dispatcher/dependency_injection.rst +++ b/docs/dispatcher/dependency_injection.rst @@ -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 `_ 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. diff --git a/examples/context_addition_from_filter.py b/examples/context_addition_from_filter.py index debd2bb9..e4eca517 100644 --- a/examples/context_addition_from_filter.py +++ b/examples/context_addition_from_filter.py @@ -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)