What problem does it solve?
Managing relationships between entities in traditional ORMs often involves complex mapping configurations and can lead to inefficient N+1 query problems. This Skill simplifies relationship handling in Koan Framework using foreign keys and efficient batch loading.
Core Features & Use Cases
- Foreign Key-Based Relationships: Define one-to-many, many-to-one, and optional relationships using simple foreign key properties.
- Navigation Helpers: Add convenient methods directly on entities (e.g.,
user.GetTodos(), todo.GetUser()) to traverse relationships.
- Prevent N+1 Queries: Implement efficient batch loading patterns to fetch related entities in a single query, dramatically improving performance for collections.
- Hierarchical Data: Easily model parent-child relationships and retrieve nested data structures.
- Use Case: Define a
User with many Todo items. Instead of fetching each user's todos individually (N+1), batch load all user IDs, then fetch all associated todos in one optimized query.
Quick Start
To define a one-to-many relationship between User and Todo:
public class User : Entity<User> { public string Name { get; set; } = ""; public Task<List<Todo>> GetTodos() => Todo.Query(t => t.UserId == Id); }
public class Todo : Entity<Todo> { public string UserId { get; set; } = ""; public Task<User?> GetUser() => User.Get(UserId); }
To prevent N+1 queries when loading multiple todos and their users:
var userIds = todos.Select(t => t.UserId).Distinct().ToArray();
var users = await User.Get(userIds); // Single query