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

Описание к видео Using the Repository Pattern for better data access encapsulation (in Python)

The code for this tutorial is available in 𝐆𝐢𝐭𝐇𝐮𝐛: https://github.com/abunuwas/repositor...

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:
👉 𝐅𝐚𝐬𝐭𝐀𝐏𝐈:    • FastAPI Tutorial  
👉 𝐒𝐐𝐋𝐀𝐥𝐜𝐡𝐞𝐦𝐲 + 𝐀𝐥𝐞𝐦𝐛𝐢𝐜:    • Setting up Alembic with SQLAlchemy  

🚨𝐖𝐀𝐑𝐍𝐈𝐍𝐆🚨 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.

I cover the Repository Pattern in-depth in 𝐜𝐡𝐚𝐩𝐭𝐞𝐫 𝟕 of my book 𝐌𝐢𝐜𝐫𝐨𝐬𝐞𝐫𝐯𝐢𝐜𝐞 𝐀𝐏𝐈𝐬. You can download chapter 7 for 𝐟𝐫𝐞𝐞 using the following link: https://www.microapis.io/resources/mi...

You can also use the following code to obtain a 𝟒𝟎% 𝐝𝐢𝐬𝐜𝐨𝐮𝐧𝐭 if you want to buy the book: 𝐬𝐥𝐩𝐞𝐫𝐚𝐥𝐭𝐚. Use the following link: http://mng.bz/0wmx.

I regularly organise live 𝐰𝐨𝐫𝐤𝐬𝐡𝐨𝐩𝐬 on software development. If you want to attend any of those workshops, check out the following page: https://microapis.io/workshops

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:
👉 𝐓𝐰𝐢𝐭𝐭𝐞𝐫:   / joseharoperalta  
👉 𝐋𝐢𝐧𝐤𝐞𝐝𝐈𝐧:   / jose-haro-peralta  

Комментарии

Информация по комментариям в разработке