2015-08-31 16:31:38 +03:00
|
|
|
"""Concept example of `Dependency Injector`."""
|
2015-03-10 01:54:05 +03:00
|
|
|
|
2015-01-04 16:54:25 +03:00
|
|
|
import sqlite3
|
2015-09-01 00:30:38 +03:00
|
|
|
import dependency_injector as di
|
2015-01-04 16:54:25 +03:00
|
|
|
|
|
|
|
|
2015-10-23 17:04:39 +03:00
|
|
|
class UsersService(object):
|
|
|
|
"""Users service, that has dependency on database."""
|
2015-03-10 01:54:05 +03:00
|
|
|
|
2015-01-04 16:54:25 +03:00
|
|
|
def __init__(self, db):
|
2015-03-10 01:54:05 +03:00
|
|
|
"""Initializer."""
|
2015-01-04 16:54:25 +03:00
|
|
|
self.db = db
|
|
|
|
|
|
|
|
|
2015-10-23 17:04:39 +03:00
|
|
|
class AuthService(object):
|
|
|
|
"""Auth service, that has dependencies on users service and database."""
|
2015-03-10 01:54:05 +03:00
|
|
|
|
2015-10-23 17:04:39 +03:00
|
|
|
def __init__(self, db, users_service):
|
2015-03-10 01:54:05 +03:00
|
|
|
"""Initializer."""
|
2015-01-04 16:54:25 +03:00
|
|
|
self.db = db
|
2015-10-23 17:04:39 +03:00
|
|
|
self.users_service = users_service
|
2015-01-04 16:54:25 +03:00
|
|
|
|
|
|
|
|
2015-11-10 11:42:29 +03:00
|
|
|
class Services(di.DeclarativeCatalog):
|
2015-10-23 17:04:39 +03:00
|
|
|
"""Catalog of service providers."""
|
2015-01-04 16:54:25 +03:00
|
|
|
|
2015-10-23 17:04:39 +03:00
|
|
|
database = di.Singleton(sqlite3.connect, ':memory:')
|
2015-10-26 13:52:52 +03:00
|
|
|
""":type: di.Provider -> sqlite3.Connection"""
|
2015-01-04 16:54:25 +03:00
|
|
|
|
2015-10-23 17:04:39 +03:00
|
|
|
users = di.Factory(UsersService,
|
|
|
|
db=database)
|
2015-10-26 13:52:52 +03:00
|
|
|
""":type: di.Provider -> UsersService"""
|
2015-01-04 16:54:25 +03:00
|
|
|
|
2015-10-23 17:04:39 +03:00
|
|
|
auth = di.Factory(AuthService,
|
|
|
|
db=database,
|
|
|
|
users_service=users)
|
2015-10-26 13:52:52 +03:00
|
|
|
""":type: di.Provider -> AuthService"""
|
2015-01-04 16:54:25 +03:00
|
|
|
|
|
|
|
|
2015-10-23 17:04:39 +03:00
|
|
|
# Retrieving catalog providers:
|
|
|
|
users_service = Services.users()
|
|
|
|
auth_service = Services.auth()
|
2015-01-11 19:10:11 +03:00
|
|
|
|
2015-10-23 17:04:39 +03:00
|
|
|
# Making some asserts:
|
|
|
|
assert users_service.db is auth_service.db is Services.database()
|
|
|
|
assert isinstance(auth_service.users_service, UsersService)
|
|
|
|
assert users_service is not Services.users()
|
|
|
|
assert auth_service is not Services.auth()
|
2015-03-23 02:04:18 +03:00
|
|
|
|
|
|
|
|
2015-10-23 17:04:39 +03:00
|
|
|
# Making some "inline" injections:
|
|
|
|
@di.inject(users_service=Services.users)
|
|
|
|
@di.inject(auth_service=Services.auth)
|
|
|
|
@di.inject(database=Services.database)
|
|
|
|
def example(users_service, auth_service, database):
|
2015-08-05 17:33:38 +03:00
|
|
|
"""Example callback."""
|
2015-10-23 17:04:39 +03:00
|
|
|
assert users_service.db is auth_service.db
|
|
|
|
assert auth_service.db is database
|
|
|
|
assert database is Services.database()
|
2015-03-23 02:04:18 +03:00
|
|
|
|
|
|
|
|
2015-10-23 17:04:39 +03:00
|
|
|
# Making a call of decorated callback:
|
2015-03-23 02:04:18 +03:00
|
|
|
example()
|