|
| 1 | +--- |
| 2 | +paths: |
| 3 | + - "src/backend/Sudoku.Domain/**/*.cs" |
| 4 | +--- |
| 5 | + |
| 6 | +# Domain Layer Guidelines |
| 7 | + |
| 8 | +## Entity Structure |
| 9 | +```csharp |
| 10 | +public class SudokuGame : AggregateRoot |
| 11 | +{ |
| 12 | + private readonly List<Cell> _cells; |
| 13 | + private readonly List<DomainEvent> _domainEvents; |
| 14 | + |
| 15 | + public GameId Id { get; private set; } |
| 16 | + public PlayerAlias PlayerAlias { get; private set; } |
| 17 | + public GameDifficulty Difficulty { get; private set; } |
| 18 | + public GameStatus Status { get; private set; } |
| 19 | + |
| 20 | + private SudokuGame() { } // Private constructor for EF Core |
| 21 | +
|
| 22 | + public static SudokuGame Create(PlayerAlias playerAlias, GameDifficulty difficulty) |
| 23 | + { |
| 24 | + // Validation and creation logic |
| 25 | + } |
| 26 | + |
| 27 | + public void MakeMove(int row, int column, int value) |
| 28 | + { |
| 29 | + // Business validation → state changes → raise domain event |
| 30 | + } |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +## Value Objects |
| 35 | +```csharp |
| 36 | +public record GameId(Guid Value) |
| 37 | +{ |
| 38 | + public static GameId New() => new(Guid.NewGuid()); |
| 39 | + public static GameId FromString(string value) => new(Guid.Parse(value)); |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +## Domain Events |
| 44 | +```csharp |
| 45 | +public record GameCreatedEvent(GameId GameId, PlayerAlias PlayerAlias, GameDifficulty Difficulty) : DomainEvent; |
| 46 | +public record MoveMadeEvent(GameId GameId, int Row, int Column, int Value) : DomainEvent; |
| 47 | +``` |
| 48 | + |
| 49 | +## Specifications |
| 50 | +```csharp |
| 51 | +public interface ISpecification<T> |
| 52 | +{ |
| 53 | + Expression<Func<T, bool>> Criteria { get; } |
| 54 | +} |
| 55 | + |
| 56 | +public class GameByPlayerSpecification : ISpecification<Game> |
| 57 | +{ |
| 58 | + private readonly PlayerAlias _playerAlias; |
| 59 | + |
| 60 | + public Expression<Func<Game, bool>> Criteria => |
| 61 | + game => game.PlayerAlias == _playerAlias; |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +## Rules |
| 66 | +- No business logic in controllers or handlers — all invariants enforced inside aggregates |
| 67 | +- Raise domain events for every significant state change |
| 68 | +- Use private setters; expose state only through domain methods |
| 69 | +- Use factory methods (`Create()`) instead of public constructors |
0 commit comments