Skip to content

Remove HTTP layer knowledge from model classes #5

@avelino

Description

@avelino

The model class currently contains direct HTTP-related knowledge, specifically in the implementation of self._client.post() calls within the model layer. This creates tight coupling between the model and the HTTP communication layer.

e.g.:

from .client import BarteClient
return BarteClient.get_instance().refund_charge(self.id, {"amount": amount} if amount else None)

Problem

This coupling:

  1. Violates the Single Responsibility Principle (SRP) - models should only be responsible for business logic and data structure
  2. Makes the code harder to test as model tests need to mock HTTP calls
  3. Reduces code reusability as the models are tightly coupled to a specific HTTP implementation
  4. Makes it difficult to change the communication layer without modifying model classes

Proposed Solution

Implement a proper separation of concerns by:

  1. Keep models focused only on data structure and business logic
  2. Use dependency injection to handle communication needs

Benefits

  • Improved testability: Models can be tested in isolation without HTTP mocking
  • Better maintainability: Changes to HTTP communication won't affect model layer
  • Enhanced flexibility: Easier to implement different communication methods
  • Cleaner architecture: Clear separation between data models and communication
  • Better adherence to SOLID principles

Implementation Notes

  • Update models to receive necessary dependencies through constructor injection
  • Move HTTP-specific logic to the service layer
  • Update tests to reflect the new architecture

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