python-dependency-injector/examples/miniapps/services/containers_alt_syntax_2.py

38 lines
1.0 KiB
Python
Raw Normal View History

"""Example of several Dependency Injector IoC containers.
2016-05-18 23:18:29 +03:00
Alternative injections definition style #2.
2016-05-18 23:18:29 +03:00
"""
import sqlite3
import boto.s3.connection
import example.services
from dependency_injector import containers
2016-05-18 23:18:29 +03:00
from dependency_injector import providers
class Platform(containers.DeclarativeContainer):
"""Container of platform service providers."""
2016-05-18 23:18:29 +03:00
database = providers.Singleton(sqlite3.connect)
2016-05-29 16:39:39 +03:00
database.add_args(':memory:')
2016-05-18 23:18:29 +03:00
s3 = providers.Singleton(boto.s3.connection.S3Connection)
2016-05-29 16:39:39 +03:00
s3.add_kwargs(aws_access_key_id='KEY',
aws_secret_access_key='SECRET')
2016-05-18 23:18:29 +03:00
class Services(containers.DeclarativeContainer):
"""Container of business service providers."""
2016-05-18 23:18:29 +03:00
users = providers.Factory(example.services.Users)
2016-05-29 16:39:39 +03:00
users.add_kwargs(db=Platform.database)
2016-05-18 23:18:29 +03:00
photos = providers.Factory(example.services.Photos)
2016-05-29 16:39:39 +03:00
photos.add_kwargs(db=Platform.database,
s3=Platform.s3)
2016-05-18 23:18:29 +03:00
auth = providers.Factory(example.services.Auth)
2016-05-29 16:39:39 +03:00
auth.add_kwargs(db=Platform.database,
token_ttl=3600)