Skip to content

Enforce Layer Boundaries in Architecture #196

@wen-templari

Description

@wen-templari

Problem Statement

The current codebase has layer boundary violations that compromise the clean architecture:

  • Router layer directly calls Repository layer, bypassing Service layer
  • Business logic mixed in Router layer instead of being encapsulated in Service layer
  • Global singletons creating tight coupling and testing difficulties

Examples of Violations

// router/member.go:96 - Direct repo call
memberId, err := repo.GetMemberIdByLogtoId(user.Id)

// router/member.go:105-117 - Business logic in router
logto_roles, err := service.LogtoServiceApp.FetchUserRole(user.Id)
mappedRole := service.MemberServiceApp.MapLogtoUserRole(logto_roles)

Proposed Solution

Implement a 4-phase plan to enforce proper layer boundaries:

Phase 1: Analysis & Interface Design (Week 1)

  • Audit current layer boundary violations
  • Create service interface definitions
  • Map business logic currently in router layer

Phase 2: Refactor Router Layer (Week 2-3)

  • Eliminate all direct repository calls from router
  • Move business logic from router to service layer
  • Standardize router handler patterns

Phase 3: Service Layer Enhancement (Week 3-4)

  • Implement dependency injection to replace global singletons
  • Consolidate business logic in service layer
  • Create proper service constructors with interface dependencies

Phase 4: Architectural Safeguards (Week 4-5)

  • Create architecture tests to prevent future violations
  • Implement CI checks for layer boundary compliance
  • Update documentation with layer boundary rules

Benefits

  • Improved Testability: Each layer can be unit tested independently
  • Better Maintainability: Clear separation of concerns
  • Reduced Coupling: Dependency injection eliminates global state
  • Consistent Patterns: Standardized approach across all handlers

Implementation Strategy

  • Service-by-Service Migration: Refactor one service at a time
  • Backward Compatibility: Maintain existing functionality during transition
  • Comprehensive Testing: Ensure no regression during refactoring

Success Criteria

  • Zero direct repository calls from router layer
  • All business logic encapsulated in service layer
  • Dependency injection implemented for all services
  • Architecture tests passing in CI
  • Documentation updated with layer boundary guidelines

Priority: High
Effort: ~4-5 weeks
Impact: Significant improvement in code maintainability and testability

Metadata

Metadata

Assignees

Type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions