Skip to content

Refactor: Introduce Domain Services & Clean API Layer for Codele#15

Merged
webreidi merged 5 commits intomainfrom
copilot/fix-8
Aug 20, 2025
Merged

Refactor: Introduce Domain Services & Clean API Layer for Codele#15
webreidi merged 5 commits intomainfrom
copilot/fix-8

Conversation

Copy link
Contributor

Copilot AI commented Aug 19, 2025

This PR introduces explicit domain services and DTO boundaries to decouple game logic from UI & hosting layers, enabling easier testing, future rule variants, and clearer API evolution.

Problem Statement

The existing Codele implementation had several architectural issues:

  • Game-specific logic resided in the Blazor component (PlayCodele.razor) and API endpoints
  • Internal game models (Guess, LetterStatus) were leaked directly via API responses
  • Adding variants (hard mode, timed mode) would require branching conditionals throughout UI/API layers
  • Game logic was tightly coupled to presentation concerns

Solution

Domain Layer Architecture

Introduced a clean domain layer in the CodeleLogic project with explicit interfaces:

  • IWordProvider: Provides target words (current implementation uses in-memory coding-related words)
  • IGuessEvaluator: Evaluates guesses against target words using existing Wordle logic
  • GameSession: Aggregate that manages game state, attempts, and completion status
  • IGameService: Orchestrates session creation and guess submission

API Layer Refactoring

Replaced direct logic endpoints with thin controllers that:

  • Call IGameService for all game operations
  • Return structured DTOs: GameSessionDto, GuessResultDto, LetterResultDto
  • Handle domain exceptions with proper HTTP status codes (400/404/409)
  • Maintain backward compatibility with legacy /codele-words endpoint

New endpoints:

POST /api/games              # Create new game session
GET /api/games/{gameId}      # Get current game state  
POST /api/games/{gameId}/guesses  # Submit a guess

Client Layer Updates

The Blazor client now:

  • Uses only DTOs from the API (no direct domain dependencies)
  • Implements async game operations with proper error handling
  • Maintains identical user experience while being fully decoupled

Benefits Delivered

  • Testability: 9 new unit tests covering all domain services and edge cases
  • Extensibility: Easy to add game variants via dependency injection
  • API Evolution: Clean contracts enable versioning without breaking changes
  • Separation of Concerns: Game logic isolated from presentation and infrastructure

Example Usage

// Domain service injection
builder.Services.AddSingleton<IWordProvider, InMemoryWordProvider>();
builder.Services.AddSingleton<IGuessEvaluator, DefaultGuessEvaluator>();
builder.Services.AddSingleton<IGameService, DefaultGameService>();

// Clean API usage
var gameSession = await gameService.CreateGameSessionAsync();
var updatedSession = await gameService.SubmitGuessAsync(gameId, "apple");

All existing game functionality works unchanged from the user perspective, with comprehensive test coverage ensuring reliability.

Fixes #8.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • aka.ms
    • Triggering command: curl -I -sSL --retry 5 --retry-delay 2 --connect-timeout 15 REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits August 19, 2025 20:15
Co-authored-by: webreidi <55603905+webreidi@users.noreply.github.com>
Co-authored-by: webreidi <55603905+webreidi@users.noreply.github.com>
Co-authored-by: webreidi <55603905+webreidi@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor: Introduce Domain Services & Clean API Layer for Codele Refactor: Introduce Domain Services & Clean API Layer for Codele Aug 19, 2025
Copilot AI requested a review from webreidi August 19, 2025 20:26
@webreidi webreidi marked this pull request as ready for review August 20, 2025 18:00
@webreidi webreidi merged commit e2ffbab into main Aug 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor: Introduce Domain Services & Clean API Layer for Codele

2 participants