mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-05-24 15:29:06 +03:00
Refactor decoupled packages example to use wiring
This commit is contained in:
parent
ae8b9dd853
commit
6d5da03bdb
|
@ -1,15 +1,26 @@
|
||||||
"""Main module."""
|
"""Main module."""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from dependency_injector.wiring import Provide
|
||||||
|
|
||||||
|
from .user.repositories import UserRepository
|
||||||
|
from .photo.repositories import PhotoRepository
|
||||||
|
from .analytics.services import AggregationService
|
||||||
from .containers import ApplicationContainer
|
from .containers import ApplicationContainer
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main(
|
||||||
application = ApplicationContainer()
|
user_repository: UserRepository = Provide[
|
||||||
application.config.from_ini('config.ini')
|
ApplicationContainer.user_package.user_repository
|
||||||
|
],
|
||||||
user_repository = application.user_package.user_repository()
|
photo_repository: PhotoRepository = Provide[
|
||||||
photo_repository = application.photo_package.photo_repository()
|
ApplicationContainer.photo_package.photo_repository
|
||||||
|
],
|
||||||
|
aggregation_service: AggregationService = Provide[
|
||||||
|
ApplicationContainer.analytics_package.aggregation_service
|
||||||
|
],
|
||||||
|
) -> None:
|
||||||
user1 = user_repository.get(id=1)
|
user1 = user_repository.get(id=1)
|
||||||
user1_photos = photo_repository.get_photos(user1.id)
|
user1_photos = photo_repository.get_photos(user1.id)
|
||||||
print(f'Retrieve user id={user1.id}, photos count={len(user1_photos)}')
|
print(f'Retrieve user id={user1.id}, photos count={len(user1_photos)}')
|
||||||
|
@ -18,11 +29,14 @@ def main() -> None:
|
||||||
user2_photos = photo_repository.get_photos(user2.id)
|
user2_photos = photo_repository.get_photos(user2.id)
|
||||||
print(f'Retrieve user id={user2.id}, photos count={len(user2_photos)}')
|
print(f'Retrieve user id={user2.id}, photos count={len(user2_photos)}')
|
||||||
|
|
||||||
aggregation_service = application.analytics_package.aggregation_service()
|
|
||||||
assert aggregation_service.user_repository is user_repository
|
assert aggregation_service.user_repository is user_repository
|
||||||
assert aggregation_service.photo_repository is photo_repository
|
assert aggregation_service.photo_repository is photo_repository
|
||||||
print('Aggregate analytics from user and photo packages')
|
print('Aggregate analytics from user and photo packages')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
application = ApplicationContainer()
|
||||||
|
application.config.from_ini('config.ini')
|
||||||
|
application.wire(modules=[sys.modules[__name__]])
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user