liveBook · Manning

Repository pattern

As explained in AI-Powered Developer: Build software with ChatGPT and Copilot

The Repository Pattern is a software design pattern that encapsulates data-access operations behind an interface separate from application logic.

The Repository Pattern separates data-access operations from application logic behind a simple interface. It hides whether data comes from a REST API, cache, file, or database. section 4.3.3

A practical implementation develops in stages:

  • Define a generic BaseRepository contract for creating, reading, updating, deleting, and listing entities. section 4.3.3
  • Implement InMemoryAssetRepository with a typed list, enabling end-to-end testing without an external database. section 4.3.3
  • Delegate AssetManager operations to the repository instead of embedding data-access details in application services. section 4.3.3
  • Replace the in-memory implementation with SQLAlchemyAssetRepository when persistence through PostgreSQL is required. section 4.3.4
Listing 4.26 Code for BaseRepository, our generic data access layer
from abc import ABC, abstractmethod
from typing import List, TypeVar, Generic
  
T = TypeVar('T')
  
class BaseRepository(ABC, Generic[T]):
    @abstractmethod
    def create(self, entity: T) -> None:
        pass
  
    @abstractmethod
    def read(self, entity_id: int) -> T:
        pass
  
    @abstractmethod
    def update(self, entity: T) -> None:
        pass
  
    @abstractmethod
    def delete(self, entity_id: int) -> None:
        pass
  
    @abstractmethod
    def get_entities(self) -> List[T]:
        pass

Use the generic abstract class as the repository contract that concrete data stores must implement. section 4.3.3

Swapping storage implementations

Application wiring can choose InMemoryAssetRepository for a lightweight run or SQLAlchemyAssetRepository with a database session for durable storage, while AssetManager remains unchanged. section 4.3.4

How AI-Powered Developer: Build software with ChatGPT and Copilot covers it

How deep it goes
Explains the abstraction, generic contract, in-memory implementation, service delegation, and database-backed replacement.
Examples
Uses BaseRepository, InMemoryAssetRepository, AssetManager, SQLAlchemyAssetRepository, and application wiring as concrete examples.
What it assumes
Assumes familiarity with application layering and CRUD while teaching repository abstractions and generic typing through an asset-management example.
What you can do after
Build it from scratch by defining a repository contract, implementing storage adapters, and switching implementations through application wiring.
How it connects
Connects CRUD operations, generics, in-memory data stores, SQLAlchemy, PostgreSQL, and hexagonal architecture.

Also explained in

Compare how 9 books explain repository pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI-Powered Developer: Build software with ChatGPT and Copilot. The section links point to the parts of the book it draws on.