0
0
Flaskframework~10 mins

Repository pattern for data access in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Repository pattern for data access
Start Application
Controller calls Repository
Repository handles Data Access
Repository queries Database
Database returns Data
Repository returns Data to Controller
Controller sends Response
The controller asks the repository for data. The repository talks to the database and returns data back. The controller then sends the response.
Execution Sample
Flask
class UserRepository:
    def get_user(self, user_id):
        return User.query.filter_by(id=user_id).first()

user_repo = UserRepository()
user = user_repo.get_user(1)
This code shows a repository class fetching a user by ID from the database.
Execution Table
StepActionEvaluationResult
1Create UserRepository instanceuser_repo = UserRepository()user_repo is ready
2Call get_user with user_id=1user_repo.get_user(1)Query User table for id=1
3Database filters users where id=1User.query.filter_by(id=1).first()Returns user object or None
4Return user object to callerreturn useruser variable holds user data
5Use user data in controlleruser = user_repo.get_user(1)user available for response
💡 Finished fetching user data, repository returns control to controller
Variable Tracker
VariableStartAfter Step 2After Step 4Final
user_repoNoneUserRepository instanceUserRepository instanceUserRepository instance
user_idNone111
userNoneUser object or NoneUser object or NoneUser object or None
Key Moments - 2 Insights
Why does the controller not access the database directly?
The controller calls the repository (see execution_table step 2) to keep data access separate and organized.
What happens if no user is found with the given ID?
The repository returns None (see execution_table step 3), so the controller can handle missing data safely.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does step 3 do?
ACreates a new user in the database
BFilters the User table for a user with id=1
CDeletes a user from the database
DReturns the controller response
💡 Hint
Check the 'Evaluation' column in step 3 for the database query action
At which step does the repository return data to the controller?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Action' and 'Result' columns to find when data is returned
If the user ID passed was 5 instead of 1, how would the execution table change?
AStep 4 would return a different repository
BStep 3 would filter by id=1 still
CStep 2 would call get_user(5) instead of get_user(1)
DNo changes in the execution table
💡 Hint
Check the 'Action' column in step 2 for the method call parameter
Concept Snapshot
Repository pattern separates data access from business logic.
Controller calls repository methods.
Repository queries database and returns data.
Keeps code organized and easier to maintain.
Example: user_repo.get_user(user_id) fetches user data.
Full Transcript
The repository pattern helps organize code by separating data access from the controller. The controller calls the repository to get data. The repository runs database queries and returns results. This keeps the controller simple and focused on handling requests and responses. In the example, the UserRepository class has a method get_user that queries the User table for a user by ID. The controller creates a UserRepository instance and calls get_user(1). The repository queries the database and returns the user object or None if not found. This pattern makes the code easier to read, test, and maintain.