mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-28 20:44:01 +03:00
c4b33749d2
* Update callable provider docs * Update coroutine provider docs * Edit object docs * Edit list provider docs * Edit configuration provider docs * Edit selector provider docs * Fix mypy stub of the ``DeclarativeContainer`` to specify the ``__init__`` interface * Edit Dependency provider docs
51 lines
1.1 KiB
Python
51 lines
1.1 KiB
Python
"""`Dependency` provider example."""
|
|
|
|
import abc
|
|
import dataclasses
|
|
|
|
from dependency_injector import containers, providers, errors
|
|
|
|
|
|
class DbAdapter(metaclass=abc.ABCMeta):
|
|
...
|
|
|
|
|
|
class SqliteDbAdapter(DbAdapter):
|
|
...
|
|
|
|
|
|
class PostgresDbAdapter(DbAdapter):
|
|
...
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class UserService:
|
|
database: DbAdapter
|
|
|
|
|
|
class Container(containers.DeclarativeContainer):
|
|
|
|
database = providers.Dependency(instance_of=DbAdapter)
|
|
|
|
user_service = providers.Factory(
|
|
UserService,
|
|
database=database,
|
|
)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
container1 = Container(database=providers.Singleton(SqliteDbAdapter))
|
|
container2 = Container(database=providers.Singleton(PostgresDbAdapter))
|
|
|
|
assert isinstance(container1.user_service().database, SqliteDbAdapter)
|
|
assert isinstance(container2.user_service().database, PostgresDbAdapter)
|
|
|
|
container3 = Container(database=providers.Singleton(object))
|
|
try:
|
|
container3.user_service()
|
|
except errors.Error as exception:
|
|
print(exception)
|
|
# The output is:
|
|
# <object object at 0x107ce5c40> is not an
|
|
# instance of <class '__main__.DbAdapter'>
|