Repository pattern

From nlelouche/Unity-SkillForge · View interactive version

Repository pattern is an AI agent skill from the GitHub repository nlelouche/Unity-SkillForge. Implements the Repository Pattern to decouple game data access from business logic. Provides a generic IRepository<T> with sync and async variants, and mock implementations for TDD.

How to use this skill

Skill file

Repository Pattern

Overview

The Repository Pattern creates an abstraction layer between the domain logic (what your game does) and the data source (where it stores data). A PlayerController asks IPlayerRepository.LoadAsync() and doesn't know — or care — whether data comes from PlayerPrefs, JSON files, PlayFab, or a mock in a unit test. This makes game systems testable, swappable, and backend-agnostic.

When to Use

Architecture

┌──────────────────────────────────────────────────────────────┐
│                    REPOSITORY PATTERN                        │
├──────────────────────────────────────────────────────────────┤
│                                                              │
│  DOMAIN LAYER            DATA LAYER                          │
│  ┌───────────────┐       ┌──────────────────────────────┐   │
│  │ GameManager   │       │ IPlayerRepository            │   │
│  │               │──────▶│  LoadAsync(): PlayerData     │   │
│  │ PlayerService │       │  SaveAsync(PlayerData)       │   │
│  └───────────────┘       │  DeleteAsync()               │   │
│                           └──────────────────────────────┘   │
│                                    │ Implemented by          │
│                  ┌─────────────────┼──────────────────┐     │
│                  ▼                 ▼                   ▼     │
│          ┌──────────────┐ ┌─────────────┐ ┌─────────────┐  │
│          │JsonRepository│ │PlayFabRepo  │ │MockRepository│  │
│          │(local files) │ │(cloud save) │ │(unit tests) │  │
│          └──────────────┘ └─────────────┘ └─────────────┘  │
└──────────────────────────────────────────────────────────────┘

TDD Contract

[Test]
public async Task PlayerRepository_WhenSaved_LoadReturnsCorrectData()
{
    // Arrange — use in-memory mock, no file I/O
    var repo = new InMemoryPlayerRepository();
    var data = new PlayerData { Level = 5, Gold = 1200 };

    // Act
    await repo.SaveAsync(data);
    var loaded = await repo.LoadAsync();

    // Assert
    Assert.AreEqual(5,    loaded.Level);
    Assert.AreEqual(1200, loaded.Gold);
}

[Test]
public async Task PlayerRepository_WhenEmpty_LoadReturnsDefault()
{
    var repo = new InMemoryPlayerRepository();
    var loaded = await repo.LoadAsync();
    Assert.IsNotNull(loaded);
    Assert.AreEqual(1, loaded.Level); // default level
}

Procedure

  1. Define Entity: Create a plain C# data class (no MonoBehaviour).
  2. Define Interface: Create IRepository<T> with async CRUD methods.
  3. Implement Concrete Repos: One per backend (JSON, PlayerPrefs, PlayFab).
  4. Create Mock Repo: InMemoryRepository<T> for unit tests.
  5. Register: Use ServiceLocator or DI container to inject the concrete repo.

Core Implementation

// --- IRepository.cs ---
using System.Threading.Tasks;

public interface IRepository<T> where T : class, new()
{
    Task<T>    LoadAsync();
    Task       SaveAsync(T data);
    Task       DeleteAsync();
    Task<bool> ExistsAsync();
}

// --- PlayerData.cs (Entity) ---
[System.Serializable]
public class PlayerData
{
    public int    Level     = 1;
    public int    Gold      = 0;
    public string PlayerName = "Hero";
}

// --- JsonPlayerRepository.cs (Local JSON) ---
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
using Newtonsoft.Json; // or JsonUtility

public class JsonPlayerRepository : IRepository<PlayerData>
{
    private readonly string _filePath;

    public JsonPlayerRepository()
    {
        _filePath = Path.Combine(Application.persistentDataPath, "player.json");
    }

    public async Task<PlayerData> LoadAsync()
    {
        if (!File.Exists(_filePath)) return new PlayerData();
        var json = await File.ReadAllTextAsync(_filePath);
        return JsonUtility.FromJson<PlayerData>(json) ?? new PlayerData();
    }

    public async Task SaveAsync(PlayerData data)
    {
        var json = JsonUtility.ToJson(data, prettyPrint: true);
        await File.WriteAllTextAsync(_filePath, json);
    }

    public Task DeleteAsync()
    {
        if (File.Exists(_filePath)) File.Delete(_filePath);
        return Task.CompletedTask;
    }

    public Task<bool> ExistsAsync() => Task.FromResult(File.Exists(_filePath));
}

// --- InMemoryPlayerRepository.cs (for TDD) ---
public class InMemoryPlayerRepository : IRepository<PlayerData>
{
    private PlayerData _store;
    public Task<PlayerData> LoadAsync()          => Task.FromResult(_store ?? new PlayerData());
    public Task            SaveAsync(PlayerData d){ _store = d; return Task.CompletedTask; }
    public Task            DeleteAsync()          { _store = null; return Task.CompletedTask; }
    public Task<bool>      ExistsAsync()          => Task.FromResult(_store != null);
}

Best Practices

Related Skills

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.