diff --git a/README.rst b/README.rst index 57ea6719..5df70f81 100644 --- a/README.rst +++ b/README.rst @@ -62,6 +62,8 @@ system that consists from several business and platform services: import sqlite3 import boto.s3.connection + + import example.main import example.services import dependency_injector.containers as containers @@ -92,29 +94,38 @@ system that consists from several business and platform services: db=Platform.database, s3=Platform.s3) -Next example demonstrates usage of ``@inject`` decorator with IoC containers -defined above: + + class Application(containers.DeclarativeContainer): + """IoC container of application component providers.""" + + main = providers.Callable(example.main.main, + users_service=Services.users, + auth_service=Services.auth, + photos_service=Services.photos) + +Next example demonstrates usage of IoC containers & providers defined above: .. code-block:: python - """Dependency Injector @inject decorator example.""" + """Run example application.""" - import application - import dependency_injector.injections as injections - - - @injections.inject(users_service=application.Services.users) - @injections.inject(auth_service=application.Services.auth) - @injections.inject(photos_service=application.Services.photos) - def main(users_service, auth_service, photos_service): - """Main function.""" - user = users_service.get_user('user') - auth_service.authenticate(user, 'secret') - photos_service.upload_photo(user['id'], 'photo.jpg') + import containers if __name__ == '__main__': - main() + containers.Application.main() + + # Previous call is an equivalent of next operations: + # + # database = sqlite3.connect(':memory:') + # s3 = boto.s3.connection.S3Connection(aws_access_key_id='KEY', + # aws_secret_access_key='SECRET') + # + # example.main.main(users_service=example.services.Users(db=database), + # auth_service=example.services.Auth(db=database, + # token_ttl=3600), + # photos_service=example.services.Photos(db=database, + # s3=s3)) Alternative definition styles ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -136,21 +147,6 @@ IoC containers from previous example could look like these: .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.Users) \ - .add_kwargs(db=Platform.database) - - auth = providers.Factory(example.services.Auth) \ - .add_kwargs(db=Platform.database, - token_ttl=3600) - - photos = providers.Factory(example.services.Photos) \ - .add_kwargs(db=Platform.database, - s3=Platform.s3) - or like this these: .. code-block:: python @@ -165,21 +161,6 @@ or like this these: 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.Users) - users.add_kwargs(db=Platform.database) - - auth = providers.Factory(example.services.Auth) - auth.add_kwargs(db=Platform.database, - token_ttl=3600) - - photos = providers.Factory(example.services.Photos) - photos.add_kwargs(db=Platform.database, - s3=Platform.s3) - You can get more *Dependency Injector* examples in ``/examples`` directory on GitHub: diff --git a/examples/miniapps/services/README.rst b/examples/miniapps/services/README.rst index 833bd44e..9fa352ae 100644 --- a/examples/miniapps/services/README.rst +++ b/examples/miniapps/services/README.rst @@ -5,4 +5,4 @@ Instructions for running .. code-block:: bash - python main.py + python run.py diff --git a/examples/miniapps/services/application.py b/examples/miniapps/services/containers.py similarity index 75% rename from examples/miniapps/services/application.py rename to examples/miniapps/services/containers.py index aa64bf1d..e12f1436 100644 --- a/examples/miniapps/services/application.py +++ b/examples/miniapps/services/containers.py @@ -2,6 +2,8 @@ import sqlite3 import boto.s3.connection + +import example.main import example.services import dependency_injector.containers as containers @@ -31,3 +33,12 @@ class Services(containers.DeclarativeContainer): photos = providers.Factory(example.services.Photos, db=Platform.database, s3=Platform.s3) + + +class Application(containers.DeclarativeContainer): + """IoC container of application component providers.""" + + main = providers.Callable(example.main.main, + users_service=Services.users, + auth_service=Services.auth, + photos_service=Services.photos) diff --git a/examples/miniapps/services/application_alt_syntax_1.py b/examples/miniapps/services/containers_alt_syntax_1.py similarity index 76% rename from examples/miniapps/services/application_alt_syntax_1.py rename to examples/miniapps/services/containers_alt_syntax_1.py index 41c82a6e..11803302 100644 --- a/examples/miniapps/services/application_alt_syntax_1.py +++ b/examples/miniapps/services/containers_alt_syntax_1.py @@ -5,6 +5,8 @@ Alternative injections definition style #1. import sqlite3 import boto.s3.connection + +import example.main import example.services import dependency_injector.containers as containers @@ -35,3 +37,12 @@ class Services(containers.DeclarativeContainer): photos = providers.Factory(example.services.Photos) \ .add_kwargs(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/application_alt_syntax_2.py b/examples/miniapps/services/containers_alt_syntax_2.py similarity index 76% rename from examples/miniapps/services/application_alt_syntax_2.py rename to examples/miniapps/services/containers_alt_syntax_2.py index d3ca39a1..8e196e62 100644 --- a/examples/miniapps/services/application_alt_syntax_2.py +++ b/examples/miniapps/services/containers_alt_syntax_2.py @@ -5,6 +5,8 @@ Alternative injections definition style #2. import sqlite3 import boto.s3.connection + +import example.main import example.services import dependency_injector.containers as containers @@ -35,3 +37,12 @@ class Services(containers.DeclarativeContainer): photos = providers.Factory(example.services.Photos) photos.add_kwargs(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/example/main.py b/examples/miniapps/services/example/main.py new file mode 100644 index 00000000..49de9e0f --- /dev/null +++ b/examples/miniapps/services/example/main.py @@ -0,0 +1,8 @@ +"""Example main module.""" + + +def main(users_service, auth_service, photos_service): + """Example main function.""" + user = users_service.get_user('user') + auth_service.authenticate(user, 'secret') + photos_service.upload_photo(user['id'], 'photo.jpg') diff --git a/examples/miniapps/services/main.py b/examples/miniapps/services/main.py deleted file mode 100644 index cab655cc..00000000 --- a/examples/miniapps/services/main.py +++ /dev/null @@ -1,18 +0,0 @@ -"""Dependency Injector @inject decorator example.""" - -import application -import dependency_injector.injections as injections - - -@injections.inject(users_service=application.Services.users) -@injections.inject(auth_service=application.Services.auth) -@injections.inject(photos_service=application.Services.photos) -def main(users_service, auth_service, photos_service): - """Main function.""" - user = users_service.get_user('user') - auth_service.authenticate(user, 'secret') - photos_service.upload_photo(user['id'], 'photo.jpg') - - -if __name__ == '__main__': - main() diff --git a/examples/miniapps/services/run.py b/examples/miniapps/services/run.py new file mode 100644 index 00000000..5c854202 --- /dev/null +++ b/examples/miniapps/services/run.py @@ -0,0 +1,19 @@ +"""Run example application.""" + +import containers + + +if __name__ == '__main__': + containers.Application.main() + + # Previous call is an equivalent of next operations: + # + # database = sqlite3.connect(':memory:') + # s3 = boto.s3.connection.S3Connection(aws_access_key_id='KEY', + # aws_secret_access_key='SECRET') + # + # example.main.main(users_service=example.services.Users(db=database), + # auth_service=example.services.Auth(db=database, + # token_ttl=3600), + # photos_service=example.services.Photos(db=database, + # s3=s3))