mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-26 03:23:58 +03:00
Update miniapps
This commit is contained in:
parent
5ecb00daba
commit
209bc518c3
39
README.rst
39
README.rst
|
@ -64,8 +64,8 @@ system that consists from several business and platform services:
|
||||||
import boto.s3.connection
|
import boto.s3.connection
|
||||||
import example.services
|
import example.services
|
||||||
|
|
||||||
from dependency_injector import containers
|
import dependency_injector.containers as containers
|
||||||
from dependency_injector import providers
|
import dependency_injector.providers as providers
|
||||||
|
|
||||||
|
|
||||||
class Platform(containers.DeclarativeContainer):
|
class Platform(containers.DeclarativeContainer):
|
||||||
|
@ -84,14 +84,14 @@ system that consists from several business and platform services:
|
||||||
users = providers.Factory(example.services.Users,
|
users = providers.Factory(example.services.Users,
|
||||||
db=Platform.database)
|
db=Platform.database)
|
||||||
|
|
||||||
photos = providers.Factory(example.services.Photos,
|
|
||||||
db=Platform.database,
|
|
||||||
s3=Platform.s3)
|
|
||||||
|
|
||||||
auth = providers.Factory(example.services.Auth,
|
auth = providers.Factory(example.services.Auth,
|
||||||
db=Platform.database,
|
db=Platform.database,
|
||||||
token_ttl=3600)
|
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
|
Next example demonstrates usage of ``@inject`` decorator with IoC containers
|
||||||
defined above:
|
defined above:
|
||||||
|
|
||||||
|
@ -99,14 +99,13 @@ defined above:
|
||||||
|
|
||||||
"""Dependency Injector @inject decorator example."""
|
"""Dependency Injector @inject decorator example."""
|
||||||
|
|
||||||
from dependency_injector.injections import inject
|
import dependency_injector.injections as di
|
||||||
|
import containers
|
||||||
from containers import Services
|
|
||||||
|
|
||||||
|
|
||||||
@inject(users_service=Services.users)
|
@di.inject(users_service=containers.Services.users)
|
||||||
@inject(auth_service=Services.auth)
|
@di.inject(auth_service=containers.Services.auth)
|
||||||
@inject(photos_service=Services.photos)
|
@di.inject(photos_service=containers.Services.photos)
|
||||||
def main(users_service, auth_service, photos_service):
|
def main(users_service, auth_service, photos_service):
|
||||||
"""Main function."""
|
"""Main function."""
|
||||||
user = users_service.get_user('user')
|
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) \
|
users = providers.Factory(example.services.Users) \
|
||||||
.add_kwargs(db=Platform.database)
|
.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) \
|
auth = providers.Factory(example.services.Auth) \
|
||||||
.add_kwargs(db=Platform.database,
|
.add_kwargs(db=Platform.database,
|
||||||
token_ttl=3600)
|
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
|
||||||
|
@ -173,14 +172,14 @@ or like this these:
|
||||||
users = providers.Factory(example.services.Users)
|
users = providers.Factory(example.services.Users)
|
||||||
users.add_kwargs(db=Platform.database)
|
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 = providers.Factory(example.services.Auth)
|
||||||
auth.add_kwargs(db=Platform.database,
|
auth.add_kwargs(db=Platform.database,
|
||||||
token_ttl=3600)
|
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:
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,8 @@ import sqlite3
|
||||||
import boto.s3.connection
|
import boto.s3.connection
|
||||||
import example.services
|
import example.services
|
||||||
|
|
||||||
from dependency_injector import containers
|
import dependency_injector.containers as containers
|
||||||
from dependency_injector import providers
|
import dependency_injector.providers as providers
|
||||||
|
|
||||||
|
|
||||||
class Platform(containers.DeclarativeContainer):
|
class Platform(containers.DeclarativeContainer):
|
||||||
|
@ -24,10 +24,10 @@ class Services(containers.DeclarativeContainer):
|
||||||
users = providers.Factory(example.services.Users,
|
users = providers.Factory(example.services.Users,
|
||||||
db=Platform.database)
|
db=Platform.database)
|
||||||
|
|
||||||
photos = providers.Factory(example.services.Photos,
|
|
||||||
db=Platform.database,
|
|
||||||
s3=Platform.s3)
|
|
||||||
|
|
||||||
auth = providers.Factory(example.services.Auth,
|
auth = providers.Factory(example.services.Auth,
|
||||||
db=Platform.database,
|
db=Platform.database,
|
||||||
token_ttl=3600)
|
token_ttl=3600)
|
||||||
|
|
||||||
|
photos = providers.Factory(example.services.Photos,
|
||||||
|
db=Platform.database,
|
||||||
|
s3=Platform.s3)
|
||||||
|
|
|
@ -7,8 +7,8 @@ import sqlite3
|
||||||
import boto.s3.connection
|
import boto.s3.connection
|
||||||
import example.services
|
import example.services
|
||||||
|
|
||||||
from dependency_injector import containers
|
import dependency_injector.containers as containers
|
||||||
from dependency_injector import providers
|
import dependency_injector.providers as providers
|
||||||
|
|
||||||
|
|
||||||
class Platform(containers.DeclarativeContainer):
|
class Platform(containers.DeclarativeContainer):
|
||||||
|
@ -28,10 +28,10 @@ class Services(containers.DeclarativeContainer):
|
||||||
users = providers.Factory(example.services.Users) \
|
users = providers.Factory(example.services.Users) \
|
||||||
.add_kwargs(db=Platform.database)
|
.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) \
|
auth = providers.Factory(example.services.Auth) \
|
||||||
.add_kwargs(db=Platform.database,
|
.add_kwargs(db=Platform.database,
|
||||||
token_ttl=3600)
|
token_ttl=3600)
|
||||||
|
|
||||||
|
photos = providers.Factory(example.services.Photos) \
|
||||||
|
.add_kwargs(db=Platform.database,
|
||||||
|
s3=Platform.s3)
|
||||||
|
|
|
@ -7,8 +7,8 @@ import sqlite3
|
||||||
import boto.s3.connection
|
import boto.s3.connection
|
||||||
import example.services
|
import example.services
|
||||||
|
|
||||||
from dependency_injector import containers
|
import dependency_injector.containers as containers
|
||||||
from dependency_injector import providers
|
import dependency_injector.providers as providers
|
||||||
|
|
||||||
|
|
||||||
class Platform(containers.DeclarativeContainer):
|
class Platform(containers.DeclarativeContainer):
|
||||||
|
@ -28,10 +28,10 @@ class Services(containers.DeclarativeContainer):
|
||||||
users = providers.Factory(example.services.Users)
|
users = providers.Factory(example.services.Users)
|
||||||
users.add_kwargs(db=Platform.database)
|
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 = providers.Factory(example.services.Auth)
|
||||||
auth.add_kwargs(db=Platform.database,
|
auth.add_kwargs(db=Platform.database,
|
||||||
token_ttl=3600)
|
token_ttl=3600)
|
||||||
|
|
||||||
|
photos = providers.Factory(example.services.Photos)
|
||||||
|
photos.add_kwargs(db=Platform.database,
|
||||||
|
s3=Platform.s3)
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
"""Dependency Injector @inject decorator example."""
|
"""Dependency Injector @inject decorator example."""
|
||||||
|
|
||||||
from dependency_injector.injections import inject
|
import dependency_injector.injections as di
|
||||||
|
import containers
|
||||||
from containers import Services
|
|
||||||
|
|
||||||
|
|
||||||
@inject(users_service=Services.users)
|
@di.inject(users_service=containers.Services.users)
|
||||||
@inject(auth_service=Services.auth)
|
@di.inject(auth_service=containers.Services.auth)
|
||||||
@inject(photos_service=Services.photos)
|
@di.inject(photos_service=containers.Services.photos)
|
||||||
def main(users_service, auth_service, photos_service):
|
def main(users_service, auth_service, photos_service):
|
||||||
"""Main function."""
|
"""Main function."""
|
||||||
user = users_service.get_user('user')
|
user = users_service.get_user('user')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user