diff options
author | Jasper Ras <jras@hostnet.nl> | 2025-01-13 13:16:06 +0100 |
---|---|---|
committer | Jasper Ras <jras@hostnet.nl> | 2025-01-13 13:16:06 +0100 |
commit | 9232b8d817d4cd4122947375156fa2fa1e9fba14 (patch) | |
tree | e4feb77f2e508f008b78f722e91488bb9a3f3806 /zettelkast/Notes/290220241458 - Unit of Work pattern.md | |
parent | ed0753ad224f0c65133bd7a63180257eecd9f5e3 (diff) |
vault backup: 2025-01-13 13:16:06
Diffstat (limited to 'zettelkast/Notes/290220241458 - Unit of Work pattern.md')
-rw-r--r-- | zettelkast/Notes/290220241458 - Unit of Work pattern.md | 40 |
1 files changed, 0 insertions, 40 deletions
diff --git a/zettelkast/Notes/290220241458 - Unit of Work pattern.md b/zettelkast/Notes/290220241458 - Unit of Work pattern.md deleted file mode 100644 index 3ba00cb..0000000 --- a/zettelkast/Notes/290220241458 - Unit of Work pattern.md +++ /dev/null @@ -1,40 +0,0 @@ -#needs-clarification - -The Unit of Work is an abstraction of the concept of atomic operations. We can have a clear boundary on what work is grouped together and should be committed only if everything succeeded. - -We can use the UoW to remove dependency on storage layer from entrypoints by having the UoW manage the set-up. This makes tests easier. - -For example an SqlAlchemyUoW can wrap an SqlAlchemy Session object and expose only its most thin API: commit and rollback. It can work together as well with a [[022920240901 - Repository Pattern|Repository]]. Using a ContextManager we can make the operations safe by only explicitly having the user of the UoW commit and otherwise when exiting do a rollback: -```py -import abc - -class AbstractUnitOfWork(abc.ABC): - products: AbstractRepository - - def __init__(self, products: AbstractRepository): - self.products = products - - def __exit__(self): - self.rollback() - - @abc.abstractmethod - def commit(self): - pass - - @abc.abstractmethod - def rollback(self): - pass - - -class SqlAlchemyUnitOfWork(AbstractUnitOfWork): - def __init__(self, products: AbstractRepository, sessionmaker): - self._session = sessionmaker() - super().__init__(some_repo) - - def commit(self): - self._session.commit() - - def rollback(self): - self._session.rollback() -``` - |