Refactor multiple container example to use wiring

This commit is contained in:
Roman Mogylatov 2020-10-06 15:38:08 -04:00
parent fdb33ca5ce
commit 609ec5cac8
2 changed files with 19 additions and 13 deletions

View File

@ -24,6 +24,6 @@ You should see:
.. code-block:: bash .. code-block:: bash
[2020-09-04 16:06:00,750] [DEBUG] [example.services.UserService]: User user@example.com has been found in database [2020-10-06 15:36:55,961] [DEBUG] [example.services.UserService]: User user@example.com has been found in database
[2020-09-04 16:06:00,750] [DEBUG] [example.services.AuthService]: User user@example.com has been successfully authenticated [2020-10-06 15:36:55,961] [DEBUG] [example.services.AuthService]: User user@example.com has been successfully authenticated
[2020-09-04 16:06:00,750] [DEBUG] [example.services.PhotoService]: Photo photo.jpg has been successfully uploaded by user user@example.com [2020-10-06 15:36:55,961] [DEBUG] [example.services.PhotoService]: Photo photo.jpg has been successfully uploaded by user user@example.com

View File

@ -2,23 +2,29 @@
import sys import sys
from dependency_injector.wiring import Provide
from .services import UserService, AuthService, PhotoService
from .containers import Application from .containers import Application
def main(email: str, password: str, photo: str) -> None: def main(
application = Application() email: str,
password: str,
application.config.from_yaml('config.yml') photo: str,
application.core.configure_logging() user_service: UserService = Provide[Application.services.user],
auth_service: AuthService = Provide[Application.services.auth],
user_service = application.services.user() photo_service: PhotoService = Provide[Application.services.photo],
auth_service = application.services.auth() ):
photo_service = application.services.photo()
user = user_service.get_user(email) user = user_service.get_user(email)
auth_service.authenticate(user, password) auth_service.authenticate(user, password)
photo_service.upload_photo(user, photo) photo_service.upload_photo(user, photo)
if __name__ == '__main__': if __name__ == '__main__':
application = Application()
application.config.from_yaml('config.yml')
application.core.configure_logging()
application.wire(modules=[sys.modules[__name__]])
main(*sys.argv[1:]) main(*sys.argv[1:])