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

View File

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