-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement separate unit of work pattern
- Loading branch information
1 parent
bdc416d
commit a83b5ef
Showing
13 changed files
with
97 additions
and
100 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from typing import Any, Callable | ||
|
||
from sqlalchemy.orm.session import Session | ||
|
||
from src.jobboard.adapters.repositories.jobs import JobSqlAlchemyRepository | ||
from src.jobboard.domain.ports.unit_of_works.jobs import JobUnitOfWorkInterface | ||
|
||
|
||
class JobSqlAlchemyUnitOfWork(JobUnitOfWorkInterface): | ||
def __init__(self, session_factory: Callable[[], Any]): | ||
self.session_factory = session_factory() | ||
|
||
def __enter__(self): | ||
self.session = self.session_factory() # type: Session | ||
self.jobs = JobSqlAlchemyRepository(self.session) | ||
return super().__enter__() | ||
|
||
def __exit__(self, *args): | ||
super().__exit__(*args) | ||
self.session.close() | ||
|
||
def _commit(self): | ||
self.session.commit() | ||
|
||
def rollback(self): | ||
self.session.rollback() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from typing import Any, Callable | ||
|
||
from sqlalchemy.orm.session import Session | ||
|
||
from src.jobboard.adapters.repositories.users import UserSqlAlchemyRepository | ||
from src.jobboard.domain.ports.unit_of_works.users import UserUnitOfWorkInterface | ||
|
||
|
||
class UserSqlAlchemyUnitOfWork(UserUnitOfWorkInterface): | ||
def __init__(self, session_factory: Callable[[], Any]): | ||
self.session_factory = session_factory() | ||
|
||
def __enter__(self): | ||
self.session = self.session_factory() # type: Session | ||
self.users = UserSqlAlchemyRepository(self.session) | ||
return super().__enter__() | ||
|
||
def __exit__(self, *args): | ||
super().__exit__(*args) | ||
self.session.close() | ||
|
||
def _commit(self): | ||
self.session.commit() | ||
|
||
def rollback(self): | ||
self.session.rollback() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import abc | ||
|
||
from src.jobboard.domain.ports.common import messagebus | ||
from src.jobboard.domain.ports.repositories.jobs import JobRepositoryInterface | ||
|
||
|
||
class JobUnitOfWorkInterface(abc.ABC): | ||
jobs: JobRepositoryInterface | ||
|
||
def __enter__(self) -> "JobUnitOfWorkInterface": | ||
return self | ||
|
||
def __exit__(self, *args): | ||
self.rollback() | ||
|
||
def commit(self): | ||
self._commit() | ||
self.publish_events() | ||
|
||
def publish_events(self): | ||
for job in self.jobs.seen: | ||
while job.events: | ||
event = job.events.pop(0) | ||
messagebus.handle(event) | ||
|
||
@abc.abstractmethod | ||
def _commit(self): | ||
raise NotImplementedError | ||
|
||
@abc.abstractmethod | ||
def rollback(self): | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters