mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-26 11:33:58 +03:00
Update miniapps/services example
This commit is contained in:
parent
81072832e4
commit
bff0e58f7f
73
README.rst
73
README.rst
|
@ -62,6 +62,8 @@ system that consists from several business and platform services:
|
||||||
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import boto.s3.connection
|
import boto.s3.connection
|
||||||
|
|
||||||
|
import example.main
|
||||||
import example.services
|
import example.services
|
||||||
|
|
||||||
import dependency_injector.containers as containers
|
import dependency_injector.containers as containers
|
||||||
|
@ -92,29 +94,38 @@ system that consists from several business and platform services:
|
||||||
db=Platform.database,
|
db=Platform.database,
|
||||||
s3=Platform.s3)
|
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
|
.. code-block:: python
|
||||||
|
|
||||||
"""Dependency Injector @inject decorator example."""
|
"""Run example application."""
|
||||||
|
|
||||||
import application
|
import containers
|
||||||
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__':
|
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
|
Alternative definition styles
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
@ -136,21 +147,6 @@ IoC containers from previous example could look like these:
|
||||||
.add_kwargs(aws_access_key_id='KEY',
|
.add_kwargs(aws_access_key_id='KEY',
|
||||||
aws_secret_access_key='SECRET')
|
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:
|
or like this these:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
@ -165,21 +161,6 @@ or like this these:
|
||||||
s3.add_kwargs(aws_access_key_id='KEY',
|
s3.add_kwargs(aws_access_key_id='KEY',
|
||||||
aws_secret_access_key='SECRET')
|
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
|
You can get more *Dependency Injector* examples in ``/examples`` directory on
|
||||||
GitHub:
|
GitHub:
|
||||||
|
|
||||||
|
|
|
@ -5,4 +5,4 @@ Instructions for running
|
||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
python main.py
|
python run.py
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import boto.s3.connection
|
import boto.s3.connection
|
||||||
|
|
||||||
|
import example.main
|
||||||
import example.services
|
import example.services
|
||||||
|
|
||||||
import dependency_injector.containers as containers
|
import dependency_injector.containers as containers
|
||||||
|
@ -31,3 +33,12 @@ class Services(containers.DeclarativeContainer):
|
||||||
photos = providers.Factory(example.services.Photos,
|
photos = providers.Factory(example.services.Photos,
|
||||||
db=Platform.database,
|
db=Platform.database,
|
||||||
s3=Platform.s3)
|
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)
|
|
@ -5,6 +5,8 @@ Alternative injections definition style #1.
|
||||||
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import boto.s3.connection
|
import boto.s3.connection
|
||||||
|
|
||||||
|
import example.main
|
||||||
import example.services
|
import example.services
|
||||||
|
|
||||||
import dependency_injector.containers as containers
|
import dependency_injector.containers as containers
|
||||||
|
@ -35,3 +37,12 @@ class Services(containers.DeclarativeContainer):
|
||||||
photos = providers.Factory(example.services.Photos) \
|
photos = providers.Factory(example.services.Photos) \
|
||||||
.add_kwargs(db=Platform.database,
|
.add_kwargs(db=Platform.database,
|
||||||
s3=Platform.s3)
|
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)
|
|
@ -5,6 +5,8 @@ Alternative injections definition style #2.
|
||||||
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import boto.s3.connection
|
import boto.s3.connection
|
||||||
|
|
||||||
|
import example.main
|
||||||
import example.services
|
import example.services
|
||||||
|
|
||||||
import dependency_injector.containers as containers
|
import dependency_injector.containers as containers
|
||||||
|
@ -35,3 +37,12 @@ class Services(containers.DeclarativeContainer):
|
||||||
photos = providers.Factory(example.services.Photos)
|
photos = providers.Factory(example.services.Photos)
|
||||||
photos.add_kwargs(db=Platform.database,
|
photos.add_kwargs(db=Platform.database,
|
||||||
s3=Platform.s3)
|
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)
|
8
examples/miniapps/services/example/main.py
Normal file
8
examples/miniapps/services/example/main.py
Normal file
|
@ -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')
|
|
@ -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()
|
|
19
examples/miniapps/services/run.py
Normal file
19
examples/miniapps/services/run.py
Normal file
|
@ -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))
|
Loading…
Reference in New Issue
Block a user