experiment with separating the UoW (whose responsibilities are
commit() , rollback() , and providing the .batches repository) from the
context manager, whose job is to initialize things, and then do the commit
or rollback on exit.
Could someone give a solution for this exercise?
Following is my code example. Would there be a point in splitting FakeUnitOfWork?
src/allocation/service_layer/unit_of_work.py
class AbstractUnitOfWork(abc.ABC):
batches: repository.AbstractRepository
@abc.abstractmethod
def commit(self):
raise NotImplementedError
@abc.abstractmethod
def rollback(self):
raise NotImplementedError
DEFAULT_SESSION_FACTORY = sessionmaker(
bind=create_engine(
config.get_postgres_uri(),
)
)
class SQLAlchemyUoWContextManager:
def __init__(self, session_factory=DEFAULT_SESSION_FACTORY):
self.session_factory = session_factory
def __enter__(self):
self.session: Session = self.session_factory()
self.uow = SqlAlchemyUnitOfWork(session=self.session)
return self.uow
def __exit__(self, *args):
self.uow.rollback()
self.session.close()
class SqlAlchemyUnitOfWork(AbstractUnitOfWork):
def __init__(self, session: Session):
self.session = session
self.batches = repository.SqlAlchemyRepository(self.session)
def commit(self):
self.session.commit()
def rollback(self):
self.session.rollback()
Could someone give a solution for this exercise?
Following is my code example. Would there be a point in splitting
FakeUnitOfWork?src/allocation/service_layer/unit_of_work.py