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.csis the supertype for all Business Services (e.g.,WepCRMService : WepService,PartnerOMGTService : WepService). - Controller Layer:
WepController.cs(and its specializations likeWepCRMController) acts as the base for MVC Controllers.
Responsibilities of WepService
- Context Access: Provides typed access to
EITServiceRequestContext(User, Database Chain). - Infrastructure Wrappers: Exposes helpers for Logging, Security Checks (
HasAccessRight), and Search Indexing (GoLucene). - Data Context: Holds the reference to
WEPV2DataContext(Entity Framework).
Benefits
- Uniformity: Every service guarantees access to the DB and User Context in the same way.
- 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
WepServicedepends on Lucene, thenSimpleFeeCalculationServicealso depends on Lucene, even if it doesn't use it.
Remediation (Future)
Prefer Composition over Inheritance.
- Instead of inheriting from
WepServiceto get the DbContext, injectIDbContextvia constructor. - Instead of inheriting
HasAccessRight, injectIAuthorizationService.