Skip to main content

ADR-007: Repository Pattern with SQLModel Integration

Context

The framework needed standardized data persistence patterns. Ad-hoc database access led to inconsistent implementations, tight coupling between business logic and database details, complex session management, and difficult testing.

Decision

Implemented a Repository Pattern with SQLModel integration providing standardized data access through BaseRepository and SQLModelRepository classes.

Key Components

  • BaseRepository: Abstract base class defining repository interface
  • SQLModelRepository: Concrete SQLModel implementation with session management
  • Context Manager Pattern: Automatic session handling with rollback on exceptions
  • Dependency Injection: Seamless integration with flavor system
  • Configuration-Driven: Database setup through config classes

Core Implementation

Consequences

Positive

  • Consistent data access patterns across flavors
  • Improved testability through repository abstraction
  • Automatic session management prevents resource leaks
  • Clear separation between data access and business logic
  • Type safety with SQLModel integration
  • Easy mocking for unit tests

Negative

  • Additional abstraction layer complexity
  • Learning curve for repository pattern concepts
  • Minimal performance overhead from context managers
  • Framework dependency on SQLModel

Usage Example

Session management is automatic with proper rollback on exceptions.