Skip to main content

Pattern: Layer Supertype

Description

The Layer Supertype pattern (defined by Martin Fowler) involves creating a base class for a specific layer of the application (e.g., Service Layer, Controller Layer) to house common logic and dependencies. All concrete classes in that layer inherit from this Supertype.

In WepNG, this is ubiquitously implemented for Services and Controllers.

Context in WepNG

  • Service Layer: WepService.cs is the supertype for all Business Services (e.g., WepCRMService : WepService, PartnerOMGTService : WepService).
  • Controller Layer: WepController.cs (and its specializations like WepCRMController) acts as the base for MVC Controllers.

Responsibilities of WepService

  1. Context Access: Provides typed access to EITServiceRequestContext (User, Database Chain).
  2. Infrastructure Wrappers: Exposes helpers for Logging, Security Checks (HasAccessRight), and Search Indexing (GoLucene).
  3. Data Context: Holds the reference to WEPV2DataContext (Entity Framework).

Benefits

  1. Uniformity: Every service guarantees access to the DB and User Context in the same way.
  2. DRY (Don't Repeat Yourself): Common infrastructure code (like checking permissions) is written once.

Risks (Anti-Pattern: The God Supertype)

  • Bloat: Over time, the Supertype attracts unrelated utility methods (e.g., specific PDF generation logic) until it becomes a "God Class".
  • Coupling: It forces every service to depend on everything the Supertype depends on. If WepService depends on Lucene, then SimpleFeeCalculationService also depends on Lucene, even if it doesn't use it.

Remediation (Future)

Prefer Composition over Inheritance.

  • Instead of inheriting from WepService to get the DbContext, inject IDbContext via constructor.
  • Instead of inheriting HasAccessRight, inject IAuthorizationService.