This repository includes a example implementation of the Unit of Work design and Repository design pattern.
For more details please check out my blog post!
---
title: Unit of Work + Repository pattern
---
classDiagram
direction LR
GenericRepository <|--GenericSqlRepository
GenericRepository <|-- HeroReposityBase
GenericRepository <|-- TeamRepositoryBase
GenericSqlRepository <|-- TeamRepository
TeamRepositoryBase <|-- TeamRepository
GenericSqlRepository <|-- HeroRepository
HeroReposityBase <|-- HeroRepository
UnitOfWorkBase <|-- UnitOfWork
HeroReposityBase *-- UnitOfWorkBase
TeamRepositoryBase *-- UnitOfWorkBase
HeroRepository *--UnitOfWork
TeamRepository *--UnitOfWork
class GenericRepository~SQLModel~{
+get_by_id(int id) SQLModel
+list(**filters) List~SQLModel~
+add(T record) SQLModel
+update(T recored) SQLModel
+delete(id int)
}
class GenericSqlRepository~SQLModel~{
-_construct_list_stmt(id)
-_construct_list_stmt(**filters)
}
class HeroReposityBase{
+get_by_name(str name) Hero
}
class HeroRepository{
}
class TeamRepositoryBase{
+get_by_name(str name) Team
}
class TeamRepository{
}
class UnitOfWorkBase{
teams: TeamRepositoryBase
heroes: HeroReposityBase
+ __enter__()
+ __exit__()
+ commit()
+ rollback()
}
class UnitOfWork{
teams: TeamRepository
heroes: HeroRepository
}
- Python 3.10
- SQLModel
git clone https://github.com/manukanne/sqlmodel-repository-pattern.git
python -m pip install -r requirements.txt
DATABASE_CONNECTION_STR=sqlite://
ℹ️ For this project an in-memory SQLite database was used, of course another database supported by SQLAlchemy can be used.
In order to run the program, simple execute the following command:
python main.py
This project is licensed under the MIT permissive license - see the Licence file for details.