Refactor aiohttp tutorial conclusion

This commit is contained in:
Roman Mogylatov 2020-10-08 21:48:17 -04:00
parent c84bbd8c51
commit 72c0c93b60

View File

@ -840,45 +840,16 @@ In this tutorial we've built an ``aiohttp`` REST API application following the d
injection principle. injection principle.
We've used the ``Dependency Injector`` as a dependency injection framework. We've used the ``Dependency Injector`` as a dependency injection framework.
The benefit you get with the ``Dependency Injector`` is the container. It starts to payoff :ref:`containers` and :ref:`providers` helped to specify how to assemble search service and
when you need to understand or change your application structure. It's easy with the container, giphy client.
cause you have everything defined explicitly in one place:
.. code-block:: python :ref:`configuration-provider` helped to deal with reading YAML file and environment variable.
"""Application containers module.""" We used :ref:`wiring` feature to inject the dependencies into the ``index()`` handler.
:ref:`provider-overriding` feature helped in testing.
from dependency_injector import containers, providers We kept all the dependencies injected explicitly. This will help when we need to add or
from dependency_injector.ext import aiohttp change something in future.
from aiohttp import web
from . import giphy, services, views
class ApplicationContainer(containers.DeclarativeContainer):
"""Application container."""
app = aiohttp.Application(web.Application)
config = providers.Configuration()
giphy_client = providers.Factory(
giphy.GiphyClient,
api_key=config.giphy.api_key,
timeout=config.giphy.request_timeout,
)
search_service = providers.Factory(
services.SearchService,
giphy_client=giphy_client,
)
index_view = aiohttp.View(
views.index,
search_service=search_service,
default_query=config.search.default_query,
default_limit=config.search.default_limit,
)
You can find complete project on the You can find complete project on the
`Github <https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/aiohttp>`_. `Github <https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/aiohttp>`_.