| name | koan-entity-first |
| description | Entity<T> patterns, GUID v7 auto-generation, static methods vs manual repositories |
Koan Entity-First Development
Core Principle
Entity replaces manual repositories. Every entity is self-aware and self-persisting. This pattern eliminates repository boilerplate while maintaining provider transparency.
Revolutionary Approach
- GUID v7 Auto-Generation: IDs generated automatically with chronological ordering
- Instance Methods:
await entity.Save(), await entity.Remove()
- Static Queries:
await Entity.All(), await Entity.Get(id), await Entity.Query()
- Provider Agnostic: Same code works across SQL, NoSQL, Vector, JSON stores
Quick Reference Card
Basic Operations
public class Todo : Entity<Todo>
{
public string Title { get; set; } = "";
public bool Completed { get; set; }
}
var todo = new Todo { Title = "Buy milk" };
await todo.Save();
var loaded = await Todo.Get(id);
var allTodos = await Todo.All();
var completed = await Todo.Query(t => t.Completed);
await todo.Remove();
Custom Keys (When Needed)
public class NumericEntity : Entity<NumericEntity, int>
{
public override int Id { get; set; }
public string Title { get; set; } = "";
}
var entity = new NumericEntity { Id = 42, Title = "Meaningful" };
await entity.Save();
Batch Operations
Batch Retrieval (Prevents N+1 Queries)
var ids = new[] { id1, id2, id3, id4 };
var todos = await Todo.Get(ids, ct);
var todos = new List<Todo?>();
foreach (var id in ids)
{
todos.Add(await Todo.Get(id, ct));
}
Use Cases:
- Collection/playlist pagination - fetch page of items by stored IDs
- Relationship navigation - fetch all related entities at once
- Bulk validation - check which IDs exist in single query
Performance: Single query vs N queries = 10-100x faster for typical datasets
Batch Persistence
var todos = Enumerable.Range(0, 1000)
.Select(i => new Todo { Title = $"Task {i}" })
.ToList();
await todos.Save();
IBatchSet<Todo, Guid> batch = Todo.Batch();
await batch
.Add(new Todo { Title = "New task" })
.Update(existingId, todo => todo.Completed = true)
.Delete(oldId)
.SaveAsync();
Pagination & Streaming
Pagination (Web APIs)
var page = await Todo.Page(pageNumber: 1, pageSize: 20);
var result = await Todo.QueryWithCount(
t => t.ProjectId == projectId,
new DataQueryOptions(
orderBy: nameof(Todo.Created),
descending: true
),
ct);
Console.WriteLine($"Showing {result.Items.Count} of {result.TotalCount}");
Streaming (Large Datasets)
await foreach (var todo in Todo.AllStream(batchSize: 1000, ct))
{
await ProcessTodo(todo);
}
await foreach (var reading in Reading.QueryStream(
"plot == 'A1'",
batchSize: 200,
ct))
{
await ProcessReading(reading);
}
When to Stream: Large datasets (>10k records), background jobs, ETL pipelines
Timestamp Auto-Update
Entities can declare [Timestamp] properties for automatic time tracking:
[Timestamp] — set once (creation time)
[Timestamp(OnSave = true)] — refreshed on every save
Works with batch operations (UpsertMany) — all entities in the batch receive the current timestamp.
Anti-Patterns to Avoid
❌ WRONG: Manual Repository Pattern
public interface ITodoRepository
{
Task<Todo> GetAsync(string id);
Task SaveAsync(Todo todo);
Task<List<Todo>> GetAllAsync();
}
public class TodoService
{
private readonly ITodoRepository _repo;
public TodoService(ITodoRepository repo) => _repo = repo;
}
Why wrong? Entity already provides all repository functionality. Manual repositories:
- Duplicate framework features
- Break provider transparency
- Add unnecessary abstraction layers
- Increase maintenance burden
✅ CORRECT: Entity Service Pattern
public class TodoService
{
public async Task<Todo> CompleteAsync(string id)
{
var todo = await Todo.Get(id);
if (todo is null)
throw new InvalidOperationException("Todo not found");
todo.Completed = true;
return await todo.Save();
}
public async Task<List<Todo>> GetCompletedAsync()
{
return await Todo.Query(t => t.Completed);
}
}
When This Skill Applies
Invoke this skill when:
- ✅ Creating new entities
- ✅ Adding data access code
- ✅ Refactoring repositories to Entity
- ✅ Building CRUD operations
- ✅ Reviewing data access patterns
- ✅ Troubleshooting entity persistence
- ✅ Optimizing queries (batch operations, streaming)
Advanced: Count Operations
var total = await Todo.Count;
var exact = await Todo.Count.Exact(ct);
var fast = await Todo.Count.Fast(ct);
var completed = await Todo.Count.Where(t => t.Completed);
Performance: Fast counts use database metadata (1000-20000x faster on large tables)
- Postgres:
pg_stat_user_tables (~5ms vs 25s for 10M rows)
- SQL Server:
sys.dm_db_partition_stats (~1ms vs 20s)
- MongoDB:
estimatedDocumentCount() (~10ms vs 15s)
When to Use:
- Fast: Pagination UI, dashboard summaries, estimates acceptable
- Exact: Critical business logic, inventory counts, reports requiring accuracy
Bundled Resources
examples/entity-crud.cs - Complete CRUD patterns
examples/entity-relationships.cs - Navigation helpers
examples/batch-operations.cs - Bulk loading and saving
anti-patterns/manual-repositories.md - What NOT to do with detailed explanations
Reference Documentation
- Full Guide:
docs/guides/entity-capabilities-howto.md
- Data Modeling:
docs/guides/data-modeling.md
- ADR: DATA-0059 (Entity-first facade decision)
- Sample:
samples/S1.Web/ (Relationship patterns)
- Sample:
samples/S0.ConsoleJsonRepo/ (Minimal 20-line example)
Framework Compliance
Entity patterns are mandatory in Koan Framework. Manual repositories break:
- Provider transparency
- Framework auto-registration
- Capability detection
- Multi-tenant context routing
Always prefer Entity patterns over custom data access abstractions.