Anti-Pattern: God Service
Description
A God Service is a service class that concentrates too many distinct responsibilities, violating the Single Responsibility Principle (SRP). instead of managing a specific domain aspect (e.g., PricingService, EmailService), it manages the entire lifecycle of an entity, including unrelated concerns.
Characteristics
- Size: Usually thousands of lines of code.
- Dependencies: Requires references to almost every other subsystem (Database, Email, FileSystem, PDF Generation, Context).
- Method Variety: Contains a mix of:
- Authorization Logic (
CanUserSeeX) - Data Access (Direct Entity Framework calls)
- Business Logic (Status changes)
- Presentation Logic (HTML string building)
- Infrastructure Logic (File I/O)
- Authorization Logic (
Evidences in Codebase
ProfilOMGTComponentService
This service acts as the "God Service" for OMGT Profiles.
- Authorization: Checks
HasAccessToProfilOMGT(Complex rules: User Role + Program Type + Coordinator Context). - Data Access: Direct EF calls (
DataCtx.ProfilOMGTs.Find). - PDF Orchestration: Calls
CommunicableServiceto generate PDFs. - Email Sending: Builds HTML strings in C# (
SendProfilesVisibleToCoordinator). - File Management: Deletes/Saves photos to disk.
Impact
- High Coupling: Changes in Email logic require modifying the Service.
- Low Cohesion: Methods dealing with Photos have nothing to do with methods dealing with Authorization.
- Testing Nightmare: Requires mocking a dozen dependencies just to test a simple logic flow.
- Performance: Often instantiates expensive resources for operations that don't need them.
Remediation
- Split by Responsibility:
ProfilReader: For querying data (Read-Only).ProfilManagerorProfilWorkflow: For state changes.ProfilNotifier: For emails.ProfilDocumentGenerator: For PDFs.
- Use Command Handlers: Isolate specific complex actions into
ExecuteXYZCommandclasses.
2026 Audit Findings
- Occurrences: 11 Services > 1,000 Lines.
- Top Offenders:
MassActionOMGTComponentService.cs(3,062 lines)TravelService.cs(2,665 lines)OrderService.cs(2,554 lines)