Update main page example and fix few typos

This commit is contained in:
Roman Mogylatov 2018-06-27 17:37:03 +03:00
parent 4d3573dd6f
commit 32a8052715
2 changed files with 44 additions and 44 deletions

View File

@ -270,9 +270,9 @@ Dependency Injector in action
----------------------------- -----------------------------
Brief example below is a simplified version of inversion of control Brief example below is a simplified version of inversion of control
containters from one of the real-life applications. This example demonstrates container from one of the real-life applications. This example demonstrates
usage of *Dependency Injector* inversion of control containers & providers usage of *Dependency Injector* inversion of control container & providers
for specifying all application components and their dependencies beetween for specifying all application components and their dependencies between
each other in one module. Besides other listed above advantages, it gives a each other in one module. Besides other listed above advantages, it gives a
great opportunity to control & manage application's structure in one place. great opportunity to control & manage application's structure in one place.
@ -292,75 +292,73 @@ great opportunity to control & manage application's structure in one place.
import dependency_injector.providers as providers import dependency_injector.providers as providers
class Core(containers.DeclarativeContainer): class IocContainer(containers.DeclarativeContainer):
"""IoC container of core component providers.""" """Application IoC container."""
config = providers.Configuration('config') config = providers.Configuration('config')
logger = providers.Singleton(logging.Logger, name='example') logger = providers.Singleton(logging.Logger, name='example')
# Gateways
class Gateways(containers.DeclarativeContainer): database_client = providers.Singleton(sqlite3.connect, config.database.dsn)
"""IoC container of gateway (API clients to remote services) providers."""
database = providers.Singleton(sqlite3.connect, Core.config.database.dsn) s3_client = providers.Singleton(
s3 = providers.Singleton(
boto3.client, 's3', boto3.client, 's3',
aws_access_key_id=Core.config.aws.access_key_id, aws_access_key_id=config.aws.access_key_id,
aws_secret_access_key=Core.config.aws.secret_access_key) aws_secret_access_key=config.aws.secret_access_key)
# Services
class Services(containers.DeclarativeContainer): users_service = providers.Factory(
"""IoC container of business service providers.""" example.services.UsersService,
db=database_client,
logger=logger)
users = providers.Factory(example.services.UsersService, auth_service = providers.Factory(
db=Gateways.database, example.services.AuthService,
logger=Core.logger) token_ttl=config.auth.token_ttl,
db=database_client,
logger=logger)
auth = providers.Factory(example.services.AuthService, photos_service = providers.Factory(
db=Gateways.database, example.services.PhotosService,
logger=Core.logger, db=database_client,
token_ttl=Core.config.auth.token_ttl) s3=s3_client,
logger=logger)
photos = providers.Factory(example.services.PhotosService, # Misc
db=Gateways.database,
s3=Gateways.s3,
logger=Core.logger)
main = providers.Callable(
class Application(containers.DeclarativeContainer): example.main.main,
"""IoC container of application component providers.""" users_service=users_service,
auth_service=auth_service,
main = providers.Callable(example.main.main, photos_service=photos_service)
users_service=Services.users,
auth_service=Services.auth,
photos_service=Services.photos)
Next example demonstrates run of example application defined above: Next example demonstrates run of example application defined above:
.. code-block:: python .. code-block:: python
"""Run example application.""" """Run example of dependency injection in Python."""
import sys import sys
import logging import logging
from containers import Core, Application from container import IocContainer
if __name__ == '__main__': if __name__ == '__main__':
# Configure platform: # Configure platform:
Core.config.override({'database': {'dsn': ':memory:'}, container = IocContainer(
'aws': {'access_key_id': 'KEY', config={'database': {'dsn': ':memory:'},
'secret_access_key': 'SECRET'}, 'aws': {'access_key_id': 'KEY',
'auth': {'token_ttl': 3600}}) 'secret_access_key': 'SECRET'},
Core.logger().addHandler(logging.StreamHandler(sys.stdout)) 'auth': {'token_ttl': 3600}})
container.logger().addHandler(logging.StreamHandler(sys.stdout))
# Run application: # Run application:
Application.main(uid=sys.argv[1], container.main(uid=sys.argv[1],
password=sys.argv[2], password=sys.argv[2],
photo=sys.argv[3]) photo=sys.argv[3])
You can get more *Dependency Injector* examples in ``/examples`` directory on You can get more *Dependency Injector* examples in ``/examples`` directory on
GitHub: GitHub:

View File

@ -9,6 +9,8 @@ follows `Semantic versioning`_
Development version Development version
------------------- -------------------
- Update main page example from "services" to "ioc_container".
- Fix few typos on main page.
- Add new example miniapp "ioc_container". - Add new example miniapp "ioc_container".
- Fix incompatibility issue between Python 3.3, pip 10.0.0 and virtualenv - Fix incompatibility issue between Python 3.3, pip 10.0.0 and virtualenv
16.0.0 (`details <https://github.com/awslabs/base64io-python/issues/4>`_) 16.0.0 (`details <https://github.com/awslabs/base64io-python/issues/4>`_)