diff --git a/README.rst b/README.rst index 2e227bc2..e182742f 100644 --- a/README.rst +++ b/README.rst @@ -64,8 +64,8 @@ system that consists from several business and platform services: import boto.s3.connection import example.services - from dependency_injector import containers - from dependency_injector import providers + import dependency_injector.containers as containers + import dependency_injector.providers as providers class Platform(containers.DeclarativeContainer): @@ -84,14 +84,14 @@ system that consists from several business and platform services: users = providers.Factory(example.services.Users, db=Platform.database) - photos = providers.Factory(example.services.Photos, - db=Platform.database, - s3=Platform.s3) - auth = providers.Factory(example.services.Auth, db=Platform.database, token_ttl=3600) + photos = providers.Factory(example.services.Photos, + db=Platform.database, + s3=Platform.s3) + Next example demonstrates usage of ``@inject`` decorator with IoC containers defined above: @@ -99,14 +99,13 @@ defined above: """Dependency Injector @inject decorator example.""" - from dependency_injector.injections import inject - - from containers import Services + import dependency_injector.injections as di + import containers - @inject(users_service=Services.users) - @inject(auth_service=Services.auth) - @inject(photos_service=Services.photos) + @di.inject(users_service=containers.Services.users) + @di.inject(auth_service=containers.Services.auth) + @di.inject(photos_service=containers.Services.photos) def main(users_service, auth_service, photos_service): """Main function.""" user = users_service.get_user('user') @@ -144,14 +143,14 @@ IoC containers from previous example could look like these: users = providers.Factory(example.services.Users) \ .add_kwargs(db=Platform.database) - photos = providers.Factory(example.services.Photos) \ - .add_kwargs(db=Platform.database, - s3=Platform.s3) - 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 @@ -173,14 +172,14 @@ or like this these: users = providers.Factory(example.services.Users) users.add_kwargs(db=Platform.database) - photos = providers.Factory(example.services.Photos) - photos.add_kwargs(db=Platform.database, - s3=Platform.s3) - 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/containers.py b/examples/miniapps/services/containers.py index 3b291d78..aa64bf1d 100644 --- a/examples/miniapps/services/containers.py +++ b/examples/miniapps/services/containers.py @@ -4,8 +4,8 @@ import sqlite3 import boto.s3.connection import example.services -from dependency_injector import containers -from dependency_injector import providers +import dependency_injector.containers as containers +import dependency_injector.providers as providers class Platform(containers.DeclarativeContainer): @@ -24,10 +24,10 @@ class Services(containers.DeclarativeContainer): users = providers.Factory(example.services.Users, db=Platform.database) - photos = providers.Factory(example.services.Photos, - db=Platform.database, - s3=Platform.s3) - auth = providers.Factory(example.services.Auth, db=Platform.database, token_ttl=3600) + + photos = providers.Factory(example.services.Photos, + db=Platform.database, + s3=Platform.s3) diff --git a/examples/miniapps/services/containers_alt_syntax_1.py b/examples/miniapps/services/containers_alt_syntax_1.py index 2b925020..41c82a6e 100644 --- a/examples/miniapps/services/containers_alt_syntax_1.py +++ b/examples/miniapps/services/containers_alt_syntax_1.py @@ -7,8 +7,8 @@ import sqlite3 import boto.s3.connection import example.services -from dependency_injector import containers -from dependency_injector import providers +import dependency_injector.containers as containers +import dependency_injector.providers as providers class Platform(containers.DeclarativeContainer): @@ -28,10 +28,10 @@ class Services(containers.DeclarativeContainer): users = providers.Factory(example.services.Users) \ .add_kwargs(db=Platform.database) - photos = providers.Factory(example.services.Photos) \ - .add_kwargs(db=Platform.database, - s3=Platform.s3) - 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) diff --git a/examples/miniapps/services/containers_alt_syntax_2.py b/examples/miniapps/services/containers_alt_syntax_2.py index 9763ac26..d3ca39a1 100644 --- a/examples/miniapps/services/containers_alt_syntax_2.py +++ b/examples/miniapps/services/containers_alt_syntax_2.py @@ -7,8 +7,8 @@ import sqlite3 import boto.s3.connection import example.services -from dependency_injector import containers -from dependency_injector import providers +import dependency_injector.containers as containers +import dependency_injector.providers as providers class Platform(containers.DeclarativeContainer): @@ -28,10 +28,10 @@ class Services(containers.DeclarativeContainer): users = providers.Factory(example.services.Users) users.add_kwargs(db=Platform.database) - photos = providers.Factory(example.services.Photos) - photos.add_kwargs(db=Platform.database, - s3=Platform.s3) - 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) diff --git a/examples/miniapps/services/main.py b/examples/miniapps/services/main.py index b6c5c864..7b484b35 100644 --- a/examples/miniapps/services/main.py +++ b/examples/miniapps/services/main.py @@ -1,13 +1,12 @@ """Dependency Injector @inject decorator example.""" -from dependency_injector.injections import inject - -from containers import Services +import dependency_injector.injections as di +import containers -@inject(users_service=Services.users) -@inject(auth_service=Services.auth) -@inject(photos_service=Services.photos) +@di.inject(users_service=containers.Services.users) +@di.inject(auth_service=containers.Services.auth) +@di.inject(photos_service=containers.Services.photos) def main(users_service, auth_service, photos_service): """Main function.""" user = users_service.get_user('user')