-
Notifications
You must be signed in to change notification settings - Fork 0
/
usecase.py
50 lines (35 loc) · 1.38 KB
/
usecase.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
39
40
41
42
43
44
45
46
47
48
49
50
from abc import ABC, abstractmethod
from typing import final, Optional, Protocol, Type, TypeVar, Union
class AsyncBaseUnitOfWork(ABC):
@abstractmethod
async def __aenter__(self) -> None:
raise NotImplementedError("required __aenter__ call for UoW Pattern")
async def __aexit__(self, exc_type: Optional[Type[Exception]], exc_val: Optional[Exception], traceback):
if exc_type:
await self.rollback()
@abstractmethod
async def commit(self):
raise NotImplementedError("Choice ORM commit func")
@abstractmethod
async def rollback(self):
raise NotImplementedError("Choice ORM rollback func")
class SyncBaseUnitOfWork(ABC):
@abstractmethod
def __enter__(self) -> None:
raise NotImplementedError("required __enter__ call for UoW Pattern")
def __exit__(self, exc_type: Optional[Type[Exception]], exc_val: Optional[Exception], traceback):
if exc_type:
self.rollback()
@abstractmethod
def commit(self):
raise NotImplementedError("Choice ORM commit func")
@abstractmethod
def rollback(self):
raise NotImplementedError("Choice ORM rollback func")
_UT = TypeVar("_UT", bound=Union[AsyncBaseUnitOfWork, SyncBaseUnitOfWork])
class BaseUseCase(Protocol[_UT]):
_uow: _UT
@property
def uow(self) -> _UT:
assert self._uow is not None
return self._uow