What problem does it solve?
Designing effective data models that encapsulate business logic, manage relationships, and enforce invariants can be complex. This Skill provides a clear, aggregate-first approach to data modeling within Koan Framework, simplifying domain-driven design.
Core Features & Use Cases
- Aggregate Boundaries: Define entities as aggregates that encapsulate their own business logic and maintain internal consistency.
- Value Objects: Use immutable value objects (e.g.,
Money, Address) to represent cohesive data, improving type safety and reducing errors.
- Lifecycle Hooks: Enforce business rules and invariants by hooking into entity lifecycle events (before save, after load, etc.).
- Navigation Helpers: Easily define and traverse relationships between entities using simple foreign keys and helper methods, without complex ORM mapping.
- Use Case: Model an
Order entity as an aggregate, ensuring its Total and Status are updated through business methods, not direct property assignments. Use a Money value object for currency, and define a lifecycle hook to prevent negative prices for Product entities.
Quick Start
To define an Order entity with a Money value object and a navigation helper:
public record Money(decimal Amount, string Currency);
public class Order : Entity<Order> {
public string CustomerId { get; set; } = "";
public Money Total { get; private set; } = new(0m, "USD");
public Task<Customer?> GetCustomer() => Customer.Get(CustomerId);
}