Skip to content

Simplify model conversions with factory pattern #93

@jonpspri

Description

@jonpspri

Problem

Complex conversions between internal models and Pydantic models (e.g., AutoSaveConfig) are currently handled through ad-hoc conversion methods scattered across the codebase. This creates maintainability issues and potential inconsistencies.

Proposed Solution

Implement a factory pattern to standardize and simplify conversions between:

  • Internal data models and Pydantic models
  • TypedDict structures and Pydantic models
  • Configuration objects and their various representations

Benefits

  • Consistency: Centralized conversion logic reduces code duplication
  • Maintainability: Single location for conversion rules makes updates easier
  • Type Safety: Factory pattern can ensure proper type validation during conversions
  • Extensibility: Easy to add new model types and conversion rules

Implementation Ideas

  1. Create a ModelFactory class with methods like:

    • from_dict() - Convert dict/TypedDict to Pydantic model
    • to_dict() - Convert Pydantic model to dict
    • convert() - Convert between different model types
  2. Use generic typing to maintain type safety

  3. Consider using protocols/interfaces for consistent conversion behavior

Examples of Current Complex Conversions

  • AutoSaveConfig conversions in session management
  • FilterCondition/FilterConditionDict handling in transformation server
  • Result model conversions across server boundaries

Acceptance Criteria

  • Design factory pattern interface
  • Implement factory for AutoSaveConfig conversions
  • Migrate existing ad-hoc conversions to use factory
  • Add comprehensive tests for factory pattern
  • Update documentation with conversion guidelines

Originally identified in PR review for type safety improvements and test coverage enhancements.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions