From a2843a974afe46e720128d233daa86fa6b4978a0 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Fri, 2 Dec 2016 20:59:12 +0200 Subject: [PATCH] Update readme and services miniapp example --- README.rst | 104 ++++-------------- examples/miniapps/services/containers.py | 31 +++--- .../services/containers_alt_syntax_1.py | 56 ---------- .../services/containers_alt_syntax_2.py | 56 ---------- examples/miniapps/services/run.py | 32 +----- 5 files changed, 48 insertions(+), 231 deletions(-) delete mode 100644 examples/miniapps/services/containers_alt_syntax_1.py delete mode 100644 examples/miniapps/services/containers_alt_syntax_2.py diff --git a/README.rst b/README.rst index 2d25d6f3..be46c071 100644 --- a/README.rst +++ b/README.rst @@ -123,36 +123,41 @@ several IoC containers for some example application: import dependency_injector.providers as providers - class Platform(containers.DeclarativeContainer): - """IoC container of platform service providers.""" + class Core(containers.DeclarativeContainer): + """IoC container of core component providers.""" configuration = providers.Configuration('config') logger = providers.Singleton(logging.Logger, name='example') - database = providers.Singleton(sqlite3.connect, configuration.database.dsn) + + class Gateways(containers.DeclarativeContainer): + """IoC container of gateway (API clients to remote services) providers.""" + + database = providers.Singleton(sqlite3.connect, + Core.configuration.database.dsn) s3 = providers.Singleton(boto.s3.connection.S3Connection, - configuration.aws.access_key_id, - configuration.aws.secret_access_key) + Core.configuration.aws.access_key_id, + Core.configuration.aws.secret_access_key) class Services(containers.DeclarativeContainer): """IoC container of business service providers.""" users = providers.Factory(example.services.UsersService, - logger=Platform.logger, - db=Platform.database) + db=Gateways.database, + logger=Core.logger) auth = providers.Factory(example.services.AuthService, - logger=Platform.logger, - db=Platform.database, - token_ttl=Platform.configuration.auth.token_ttl) + db=Gateways.database, + logger=Core.logger, + token_ttl=Core.configuration.auth.token_ttl) photos = providers.Factory(example.services.PhotosService, - logger=Platform.logger, - db=Platform.database, - s3=Platform.s3) + db=Gateways.database, + s3=Gateways.s3, + logger=Core.logger) class Application(containers.DeclarativeContainer): @@ -172,83 +177,22 @@ Next example demonstrates run of example application defined above: import sys import logging - from containers import Platform, Application + from containers import Core, Application if __name__ == '__main__': # Configure platform: - Platform.configuration.update({'database': {'dsn': ':memory:'}, - 'aws': {'access_key_id': 'KEY', - 'secret_access_key': 'SECRET'}, - 'auth': {'token_ttl': 3600}}) - Platform.logger().addHandler(logging.StreamHandler(sys.stdout)) + Core.configuration.update({'database': {'dsn': ':memory:'}, + 'aws': {'access_key_id': 'KEY', + 'secret_access_key': 'SECRET'}, + 'auth': {'token_ttl': 3600}}) + Core.logger().addHandler(logging.StreamHandler(sys.stdout)) # Run application: Application.main(uid=sys.argv[1], password=sys.argv[2], photo=sys.argv[3]) - # Previous call is an equivalent of next operations: - # - # logger = logging.Logger(name='example') - # database = sqlite3.connect(':memory:') - # s3 = boto.s3.connection.S3Connection(aws_access_key_id='KEY', - # aws_secret_access_key='SECRET') - # - # example.main.main( - # uid=sys.argv[1], - # password=sys.argv[2], - # photo=sys.argv[3], - # users_service=example.services.UsersService(logger=logger, - # db=database), - # auth_service=example.services.AuthService(logger=logger, - # db=database, - # token_ttl=3600), - # photos_service=example.services.PhotosService(logger=logger, - # db=database, - # s3=s3)) - -Alternative definition styles of providers -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -*Dependecy Injector* supports few other styles of dependency injections -definition. - -IoC containers from previous example could look like these: - -.. code-block:: python - - class Platform(containers.DeclarativeContainer): - """IoC container of platform service providers.""" - - logger = providers.Singleton(logging.Logger) \ - .add_kwargs(name='example') - - database = providers.Singleton(sqlite3.connect) \ - .add_args(':memory:') - - s3 = providers.Singleton(boto.s3.connection.S3Connection) \ - .add_kwargs(aws_access_key_id='KEY', - aws_secret_access_key='SECRET') - -or like this these: - -.. code-block:: python - - class Platform(containers.DeclarativeContainer): - """IoC container of platform service providers.""" - - logger = providers.Singleton(logging.Logger) - logger.add_kwargs(name='example') - - database = providers.Singleton(sqlite3.connect) - database.add_args(':memory:') - - s3 = providers.Singleton(boto.s3.connection.S3Connection) - s3.add_kwargs(aws_access_key_id='KEY', - aws_secret_access_key='SECRET') - - You can get more *Dependency Injector* examples in ``/examples`` directory on GitHub: diff --git a/examples/miniapps/services/containers.py b/examples/miniapps/services/containers.py index a8872c4a..6b841a4d 100644 --- a/examples/miniapps/services/containers.py +++ b/examples/miniapps/services/containers.py @@ -12,36 +12,41 @@ import dependency_injector.containers as containers import dependency_injector.providers as providers -class Platform(containers.DeclarativeContainer): - """IoC container of platform service providers.""" +class Core(containers.DeclarativeContainer): + """IoC container of core component providers.""" configuration = providers.Configuration('config') logger = providers.Singleton(logging.Logger, name='example') - database = providers.Singleton(sqlite3.connect, configuration.database.dsn) + +class Gateways(containers.DeclarativeContainer): + """IoC container of gateway (API clients to remote services) providers.""" + + database = providers.Singleton(sqlite3.connect, + Core.configuration.database.dsn) s3 = providers.Singleton(boto.s3.connection.S3Connection, - configuration.aws.access_key_id, - configuration.aws.secret_access_key) + Core.configuration.aws.access_key_id, + Core.configuration.aws.secret_access_key) class Services(containers.DeclarativeContainer): """IoC container of business service providers.""" users = providers.Factory(example.services.UsersService, - logger=Platform.logger, - db=Platform.database) + db=Gateways.database, + logger=Core.logger) auth = providers.Factory(example.services.AuthService, - logger=Platform.logger, - db=Platform.database, - token_ttl=Platform.configuration.auth.token_ttl) + db=Gateways.database, + logger=Core.logger, + token_ttl=Core.configuration.auth.token_ttl) photos = providers.Factory(example.services.PhotosService, - logger=Platform.logger, - db=Platform.database, - s3=Platform.s3) + db=Gateways.database, + s3=Gateways.s3, + logger=Core.logger) class Application(containers.DeclarativeContainer): diff --git a/examples/miniapps/services/containers_alt_syntax_1.py b/examples/miniapps/services/containers_alt_syntax_1.py deleted file mode 100644 index 5910bda7..00000000 --- a/examples/miniapps/services/containers_alt_syntax_1.py +++ /dev/null @@ -1,56 +0,0 @@ -"""Example of dependency injection in Python. - -Alternative injections definition style #1. -""" - -import logging -import sqlite3 - -import boto.s3.connection - -import example.main -import example.services - -import dependency_injector.containers as containers -import dependency_injector.providers as providers - - -class Platform(containers.DeclarativeContainer): - """IoC container of platform service providers.""" - - logger = providers.Singleton(logging.Logger) \ - .add_kwargs(name='example') - - database = providers.Singleton(sqlite3.connect) \ - .add_args(':memory:') - - s3 = providers.Singleton(boto.s3.connection.S3Connection) \ - .add_kwargs(aws_access_key_id='KEY', - aws_secret_access_key='SECRET') - - -class Services(containers.DeclarativeContainer): - """IoC container of business service providers.""" - - users = providers.Factory(example.services.UsersService) \ - .add_kwargs(logger=Platform.logger, - db=Platform.database) - - auth = providers.Factory(example.services.AuthService) \ - .add_kwargs(logger=Platform.logger, - db=Platform.database, - token_ttl=3600) - - photos = providers.Factory(example.services.PhotosService) \ - .add_kwargs(logger=Platform.logger, - db=Platform.database, - s3=Platform.s3) - - -class Application(containers.DeclarativeContainer): - """IoC container of application component providers.""" - - main = providers.Callable(example.main.main) \ - .add_kwargs(users_service=Services.users, - auth_service=Services.auth, - photos_service=Services.photos) diff --git a/examples/miniapps/services/containers_alt_syntax_2.py b/examples/miniapps/services/containers_alt_syntax_2.py deleted file mode 100644 index cf332672..00000000 --- a/examples/miniapps/services/containers_alt_syntax_2.py +++ /dev/null @@ -1,56 +0,0 @@ -"""Example of dependency injection in Python. - -Alternative injections definition style #2. -""" - -import logging -import sqlite3 - -import boto.s3.connection - -import example.main -import example.services - -import dependency_injector.containers as containers -import dependency_injector.providers as providers - - -class Platform(containers.DeclarativeContainer): - """IoC container of platform service providers.""" - - logger = providers.Singleton(logging.Logger) - logger.add_kwargs(name='example') - - database = providers.Singleton(sqlite3.connect) - database.add_args(':memory:') - - s3 = providers.Singleton(boto.s3.connection.S3Connection) - s3.add_kwargs(aws_access_key_id='KEY', - aws_secret_access_key='SECRET') - - -class Services(containers.DeclarativeContainer): - """IoC container of business service providers.""" - - users = providers.Factory(example.services.UsersService) - users.add_kwargs(logger=Platform.logger, - db=Platform.database) - - auth = providers.Factory(example.services.AuthService) - auth.add_kwargs(logger=Platform.logger, - db=Platform.database, - token_ttl=3600) - - photos = providers.Factory(example.services.PhotosService) - photos.add_kwargs(logger=Platform.logger, - db=Platform.database, - s3=Platform.s3) - - -class Application(containers.DeclarativeContainer): - """IoC container of application component providers.""" - - main = providers.Callable(example.main.main) - main.add_kwargs(users_service=Services.users, - auth_service=Services.auth, - photos_service=Services.photos) diff --git a/examples/miniapps/services/run.py b/examples/miniapps/services/run.py index 662fb684..acd37ace 100644 --- a/examples/miniapps/services/run.py +++ b/examples/miniapps/services/run.py @@ -3,38 +3,18 @@ import sys import logging -from containers import Platform, Application +from containers import Core, Application if __name__ == '__main__': # Configure platform: - Platform.configuration.update({'database': {'dsn': ':memory:'}, - 'aws': {'access_key_id': 'KEY', - 'secret_access_key': 'SECRET'}, - 'auth': {'token_ttl': 3600}}) - Platform.logger().addHandler(logging.StreamHandler(sys.stdout)) + Core.configuration.update({'database': {'dsn': ':memory:'}, + 'aws': {'access_key_id': 'KEY', + 'secret_access_key': 'SECRET'}, + 'auth': {'token_ttl': 3600}}) + Core.logger().addHandler(logging.StreamHandler(sys.stdout)) # Run application: Application.main(uid=sys.argv[1], password=sys.argv[2], photo=sys.argv[3]) - - # Previous call is an equivalent of next operations: - # - # logger = logging.Logger(name='example') - # database = sqlite3.connect(':memory:') - # s3 = boto.s3.connection.S3Connection(aws_access_key_id='KEY', - # aws_secret_access_key='SECRET') - # - # example.main.main( - # uid=sys.argv[1], - # password=sys.argv[2], - # photo=sys.argv[3], - # users_service=example.services.UsersService(logger=logger, - # db=database), - # auth_service=example.services.AuthService(logger=logger, - # db=database, - # token_ttl=3600), - # photos_service=example.services.PhotosService(logger=logger, - # db=database, - # s3=s3))