Update concept example

This commit is contained in:
Roman Mogilatov 2015-11-23 19:09:35 +02:00
parent 8d0b82cf36
commit 81f278d3dc
2 changed files with 34 additions and 28 deletions

View File

@ -67,7 +67,10 @@ Examples
"""Concept example of `Dependency Injector`."""
import sqlite3
import dependency_injector as di
from dependency_injector import catalogs
from dependency_injector import providers
from dependency_injector import injections
class UsersService(object):
@ -87,20 +90,20 @@ Examples
self.users_service = users_service
class Services(di.DeclarativeCatalog):
class Services(catalogs.DeclarativeCatalog):
"""Catalog of service providers."""
database = di.Singleton(sqlite3.connect, ':memory:')
""":type: di.Provider -> sqlite3.Connection"""
database = providers.Singleton(sqlite3.connect, ':memory:')
""":type: providers.Provider -> sqlite3.Connection"""
users = di.Factory(UsersService,
db=database)
""":type: di.Provider -> UsersService"""
users = providers.Factory(UsersService,
db=database)
""":type: providers.Provider -> UsersService"""
auth = di.Factory(AuthService,
db=database,
users_service=users)
""":type: di.Provider -> AuthService"""
auth = providers.Factory(AuthService,
db=database,
users_service=users)
""":type: providers.Provider -> AuthService"""
# Retrieving catalog providers:
@ -115,9 +118,9 @@ Examples
# Making some "inline" injections:
@di.inject(users_service=Services.users)
@di.inject(auth_service=Services.auth)
@di.inject(database=Services.database)
@injections.inject(users_service=Services.users)
@injections.inject(auth_service=Services.auth)
@injections.inject(database=Services.database)
def example(users_service, auth_service, database):
"""Example callback."""
assert users_service.db is auth_service.db

View File

@ -1,7 +1,10 @@
"""Concept example of `Dependency Injector`."""
import sqlite3
import dependency_injector as di
from dependency_injector import catalogs
from dependency_injector import providers
from dependency_injector import injections
class UsersService(object):
@ -21,20 +24,20 @@ class AuthService(object):
self.users_service = users_service
class Services(di.DeclarativeCatalog):
class Services(catalogs.DeclarativeCatalog):
"""Catalog of service providers."""
database = di.Singleton(sqlite3.connect, ':memory:')
""":type: di.Provider -> sqlite3.Connection"""
database = providers.Singleton(sqlite3.connect, ':memory:')
""":type: providers.Provider -> sqlite3.Connection"""
users = di.Factory(UsersService,
db=database)
""":type: di.Provider -> UsersService"""
users = providers.Factory(UsersService,
db=database)
""":type: providers.Provider -> UsersService"""
auth = di.Factory(AuthService,
db=database,
users_service=users)
""":type: di.Provider -> AuthService"""
auth = providers.Factory(AuthService,
db=database,
users_service=users)
""":type: providers.Provider -> AuthService"""
# Retrieving catalog providers:
@ -49,9 +52,9 @@ assert auth_service is not Services.auth()
# Making some "inline" injections:
@di.inject(users_service=Services.users)
@di.inject(auth_service=Services.auth)
@di.inject(database=Services.database)
@injections.inject(users_service=Services.users)
@injections.inject(auth_service=Services.auth)
@injections.inject(database=Services.database)
def example(users_service, auth_service, database):
"""Example callback."""
assert users_service.db is auth_service.db