Skip to main content

Anti-Pattern: Schizophrenic Architecture

Description

The "Schizophrenic Architecture" occurs when a codebase cannot decide on a single data access strategy, leading to a mix of conflicting patterns within the same logical layer (or even the same method).

In WepNG_BO, this manifests as Controllers arbitrary choosing between:

  1. Direct Entity Framework Access: Instantiating WEPV2DataContext directly and querying DbSets.
  2. Service Layer Access: Instantiating ComponentServices (e.g., CenterComponentService) to perform actions.

Evidence

  • Direct Access: CatalogueController.Index uses db.Catalogues.Include(...).
  • Service Access: CenterController.Index uses new CenterNGComponentService(...).
  • Mixed Mode: CenterController methods often mix db.Find() with service calls.

Impact

  • Transaction Scope Ambiguity: If a controller opens a transaction via the Service, but then reads/writes using its own db context, data consistency is not guaranteed.
  • Testing Impossibility: We cannot mock the Data Access because it's hardcoded in the Controller (new WEPV2DataContext()).
  • Cognitive Load: Developers must check every method to see "how" it talks to the DB.

Remediation

  • Strict Layering: Controllers MUST mostly talk to Services/Handlers (Query or Command).
  • Dependency Injection: Never new DbContext() in a Controller.
  • Refactoring Rule: When touching a Controller, if it has db.Something, move that logic to a Request Handler or Service.