-
Notifications
You must be signed in to change notification settings - Fork 18
/
example-02.py
38 lines (25 loc) · 950 Bytes
/
example-02.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
This example illustrates a basic usage of the Container class to register
a concrete type by base type, and its activation by base type.
This pattern helps writing code that is decoupled (e.g. business layer logic separated
from exact implementations of data access logic).
"""
from abc import ABC, abstractmethod
from dataclasses import dataclass
from rodi import Container
@dataclass
class Cat:
id: str
name: str
class CatsRepository(ABC):
@abstractmethod
def get_cat(self, cat_id: str) -> Cat:
"""Gets information of a cat by ID."""
class SQLiteCatsRepository(CatsRepository):
def get_cat(self, cat_id: str) -> Cat:
"""Gets information of a cat by ID, from a source SQLite DB."""
raise NotImplementedError()
container = Container()
container.register(CatsRepository, SQLiteCatsRepository)
example_1 = container.resolve(CatsRepository)
assert isinstance(example_1, SQLiteCatsRepository)