Skip to main content

Anti-Pattern: God Entity (Huge Partial Classes)

Description

A single Entity class that has grown to thousands of lines of code (LOC), often through the abuse of partial classes to bypass file size limits. It typically contains business logic, data access helpers, and formatting logic that should belong to Services or DTOs.

Impact

  • Maintainability: impossible to understand the full state of the object.
  • Memory: Loading this entity might pull in a massive graph of related data if lazy loading is triggered on its many properties.
  • Coupling: Changes to this entity affect unrelated parts of the system (e.g., changes for "Printing" break "Invoicing").

Detection

  • File Size: Files > 2000 LOC.
  • Top Offenders:
    • OrderTravelProductOMGT.cs: ~5100 LOC
    • OrderOMGT.cs: ~5000 LOC
    • WepKeyNoteModelNGFactory.cs: ~3500 LOC

Refactoring

Extract Class Refactoring:

  1. Identify cohesive clusters of methods (e.g., all methods related to "Invoicing").
  2. Move them to a Extension Method class (e.g., OrderInvoicingExtensions.cs) or a dedicated Domain Service (e.g., OrderInvoicingService.cs).
  3. Stop adding logic to the Entity. Use DTOs for UI/API interaction.