Skip to content

v0.4.0 Task 1.2.3: Generic Repository Base Implementations #141

@gbrennon

Description

@gbrennon

Task Description

Epic: Epic 1.2: Repository Pattern Completion (#70)
Acceptance Criteria: Generic Repository base implementations, InMemoryRepository for testing, Specification-based querying support, Unit of Work integration

Implementation Details

Files to Create/Modify

  • src/forging_blocks/infrastructure/repositories/generic_repository.py (NEW)
  • src/forging_blocks/infrastructure/repositories/in_memory_repository.py (NEW)
  • tests/unit/infrastructure/repositories/test_generic_repository.py (NEW)

Generic Repository Interface

from abc import ABC, abstractmethod
from typing import TypeVar, Generic, List, Optional, AsyncIterator
from uuid import UUID

TEntity = TypeVar('TEntity')
TId = TypeVar('TId')

class GenericRepository(ABC, Generic[TEntity, TId]):
    """Generic repository interface with specification support."""
    
    @abstractmethod
    async def save(self, entity: TEntity) -> None:
        pass
    
    @abstractmethod
    async def get_by_id(self, id: TId) -> Optional[TEntity]:
        pass
    
    @abstractmethod
    async def find_by_specification(self, spec: Specification[TEntity]) -> List[TEntity]:
        pass
    
    @abstractmethod
    async def delete(self, entity: TEntity) -> None:
        pass

In-Memory Implementation

class InMemoryRepository(GenericRepository[TEntity, TId]):
    """In-memory repository for testing and simple scenarios."""
    
    def __init__(self):
        self._storage: Dict[TId, TEntity] = {}
    
    async def save(self, entity: TEntity) -> None:
        self._storage[entity.id] = entity
    
    async def get_by_id(self, id: TId) -> Optional[TEntity]:
        return self._storage.get(id)
    
    async def find_by_specification(self, spec: Specification[TEntity]) -> List[TEntity]:
        return [entity for entity in self._storage.values() if spec.is_satisfied_by(entity)]

Acceptance Criteria

  • Generic repository interface with full CRUD operations
  • In-memory implementation for testing scenarios
  • Specification-based querying integration
  • Unit of Work pattern support
  • Type safety with generic type parameters
  • Comprehensive tests covering all operations

Definition of Done

  • Generic repository working with your Entity base class
  • In-memory implementation ready for testing
  • Specification integration with repository queries
  • All tests passing with >95% coverage
  • Documentation with usage examples

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions