Skip to main content

Anti-Pattern: God Controller (aka Fat Controller)

Description

A "God Controller" (also known as a Fat Controller) is a Controller class that knows too much and does too much. Instead of being a thin layer that acts as a traffic cop (Receive Request -> Validate -> Delegate -> Return Response), it contains massive amounts of:

  • Business Logic: Calculating prices, filtering data, applying complex rules.
  • Presentation Logic: Building HTML strings, formatting dates manually.
  • Session Management: Directly reading/writing Session or TempData.
  • Data Access: Querying the database directly.

Evidence

  • File Size: CenterController.cs is Lines of Code: 800+.
  • Responsibilities:
    • Handles UnlockCenterCache (Cache Mgt).
    • Handles HtmlBuilder (Presentation).
    • Handles Duplicate (Deep Cloning Logic).
    • Handles CompareCenterYear (Complex Reporting Logic).
  • Leaked Abstractions: It manually manages file uploads (HttpPostedFileBase) mixed with Entity logic.

Impact

  • Untestable Code: You cannot unit test CompareCenterYear without mocking the entire HttpContext, Database, and Filesystem.
  • No Reuse: The logic inside the controller cannot be used by a background job, an API, or a different UI.
  • Maintenance Nightmare: Changing one small thing risks breaking unrelated features sharing the same file.

Remediation

  • Extract Delegators: Move logic to CenterOrchestrator or CenterService.
  • MediatR / CQS: New features should use a Handler pattern (e.g., DuplicateCenterCommandHandler).
  • API First: Write the logic as if it were for an API. The Controller should just call _api.GetCenter(...).

2026 Audit Findings

  • Occurrences: 14 Controllers > 800 Lines.
  • Top Offenders:
    • HomeController.cs (1,820 lines)
    • ProspectController.cs (1,670 lines)
    • WepHomeController.cs (1,562 lines)
    • OrderController.cs (1,355 lines)