Skip to content

Commit 272b65c

Browse files
committed
wrapper pattern for repository
1 parent a6bc598 commit 272b65c

2 files changed

Lines changed: 24 additions & 19 deletions

File tree

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
1-
import abc
2-
from typing import Set
1+
from typing import Set, Protocol
32
from allocation.domain import model
43

54

6-
class AbstractRepository(abc.ABC):
75

8-
def __init__(self):
6+
class AbstractRepository(Protocol):
7+
8+
def add(self, product: model.Product):
9+
...
10+
11+
def get(self, sku) -> model.Product:
12+
...
13+
14+
15+
16+
class TrackingRepository:
17+
seen: Set[model.Product]
18+
19+
def __init__(self, repo: AbstractRepository):
920
self.seen = set() # type: Set[model.Product]
21+
self._repo = repo
1022

1123
def add(self, product: model.Product):
12-
self._add(product)
24+
self._repo.add(product)
1325
self.seen.add(product)
1426

1527
def get(self, sku) -> model.Product:
16-
product = self._get(sku)
28+
product = self._repo.get(sku)
1729
if product:
1830
self.seen.add(product)
1931
return product
2032

21-
@abc.abstractmethod
22-
def _add(self, product: model.Product):
23-
raise NotImplementedError
24-
25-
@abc.abstractmethod
26-
def _get(self, sku) -> model.Product:
27-
raise NotImplementedError
28-
2933

3034

31-
class SqlAlchemyRepository(AbstractRepository):
35+
class SqlAlchemyRepository:
3236

3337
def __init__(self, session):
34-
super().__init__()
3538
self.session = session
3639

37-
def _add(self, product):
40+
def add(self, product):
3841
self.session.add(product)
3942

40-
def _get(self, sku):
43+
def get(self, sku):
4144
return self.session.query(model.Product).filter_by(sku=sku).first()

src/allocation/service_layer/unit_of_work.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ def __init__(self, session_factory=DEFAULT_SESSION_FACTORY):
5353

5454
def __enter__(self):
5555
self.session = self.session_factory() # type: Session
56-
self.products = repository.SqlAlchemyRepository(self.session)
56+
self.products = repository.TrackingRepository(
57+
repository.SqlAlchemyRepository(self.session)
58+
)
5759
return super().__enter__()
5860

5961
def __exit__(self, *args):

0 commit comments

Comments
 (0)