filmov
tv
Using the Repository Pattern for better data access encapsulation (in Python)

ะะพะบะฐะทะฐัั ะพะฟะธัะฐะฝะธะต
This tutorial explains
๐ What the Repository Pattern is
๐ When to use the Repository Pattern
๐ How to implement the Repository Pattern
You'll learn to use the Repository Pattern by refactoring an application in which the data access layer is tightly coupled with the API layer. To keep things simple, we use a SQLite database for demonstration, but the same strategies can be applied with any other database engine.
The starter code is an API built with FastAPI and it uses SQLAlchemy to manage interactions with the database. The video doesn't explain how FastAPI and SQLAlchemy work, so if you need a ๐ซ๐๐๐ซ๐๐ฌ๐ก๐๐ซ, check out the following videos:
๐จ๐๐๐๐๐๐๐๐จ The content in this tutorial is fairly ๐๐๐ฏ๐๐ง๐๐๐. If you're a ๐๐๐ ๐ข๐ง๐ง๐๐ซ developer, you can still benefit from watching this video, but don't get frustrated if not everything makes sense. I highly recommend you ๐ฉ๐ซ๐๐๐ญ๐ข๐๐ along with the code, and try out all the commands I run in the terminal. If you encounter any issues with the code or something doesn't make sense, feel free to ping me or feel free to raise an issue in the GitHub repo for this video. My contact details are at the bottom of this description.
๐ก๐๐ข๐ฌ๐๐ฅ๐จ๐ฌ๐ฎ๐ซ๐๐ก The implementation of the Repository Pattern shown in this video is fairly standard. The idea of the repositories registry is my opinionated. You don't need to use both patterns together. The strategy used to make dependencies injectable is also opinionated, and you may be able to use alternative dependency injection strategies. These are the strategies and patterns that I generally use with my clients.
Chapters:
0:00 Introduction
0:29 Startup code and clone and checkout
1:22 Presenting the startup code, API and db models
3:00 How the API is tightly coupled to the data layer and why tight coupling is bad
4:32 How repository helps avoid tight coupling with the data layer and what are the benefits
6:50 How do you implement the repository pattern?
8:45 Implementing the bookings repository
10:22 Moving database logic from the API layer to the repository
11:41 Using the repository in the API layer
12:50 Setting up the environment, installing the dependencies
13:30 Running the database migrations
13:54 Running the server and testing the changes using the Swagger UI
15:07 Introducing the test suite and running it
16:12 Making SQLAlchemy's session maker an injectable dependency
18:15 Testing the changes
18:41 Updating the test suite to inject SQLAlchemy's session maker
19:32 Mocking SQLAlchemy's session maker
20:06 Adding a bookings business object
25:07 Returning business objects from the repository
27:12 Testing the changes
27:50 Injecting the repositories into the FastAPI app using a registry
30:58 Injecting a dummy repository in the test and running the test suite
33:19 Wrapping up (show images of the books here)
๐๐ก๐๐ญ ๐ข๐ฌ ๐ญ๐ก๐ ๐๐๐ฉ๐จ๐ฌ๐ข๐ญ๐จ๐ซ๐ฒ ๐๐๐ญ๐ญ๐๐ซ๐ง?
Repository is ๐๐๐ญ๐ ๐๐๐๐๐ฌ๐ฌ ๐๐๐ฌ๐ญ๐ซ๐๐๐ญ๐ข๐จ๐ง pattern. It helps you build a bridge between your business layer and your data access layer. Typically, Repository offers an in-memory list or ๐๐จ๐ฅ๐ฅ๐๐๐ญ๐ข๐จ๐ง-like interface to your data. It ๐๐ง๐๐๐ฉ๐ฌ๐ฎ๐ฅ๐๐ญ๐๐ฌ the implementation details of your data management system, hence ๐๐๐๐จ๐ฎ๐ฉ๐ฅ๐ข๐ง๐ your business layer from your databases. By using a repository, saving or deleting data from your database looks like adding or removing elements from a list. Using the Repository pattern makes it easier to maintain, test, and debug all the layers of your application in isolation from the database layer.
If you have any ๐ช๐ฎ๐๐ฌ๐ญ๐ข๐จ๐ง๐ฌ, or if you just would like to ๐๐จ๐ง๐ง๐๐๐ญ with me, feel free to reach out to me in any of the following platforms:
ะะพะผะผะตะฝัะฐัะธะธ