Design Patterns
What is Active Record?
Active Record in CQL
struct User
include CQL::ActiveRecord::Model(Int64)
db_context MyDB, :users
property id : Int64?
property name : String
property email : String
property active : Bool = true
# Business logic in the model
def activate!
@active = true
save
end
def deactivate!
@active = false
save
end
end
# Usage - the model handles its own persistence
user = User.new("John", "john@example.com")
user.save # Model saves itself
user.activate! # Business logic + persistence togetherKey Characteristics
1. Self-Aware Persistence
2. Class Methods for Queries
3. Built-in Validations
4. Lifecycle Callbacks
Benefits
Simplicity
Rapid Development
Discoverability
Trade-offs
Coupling
Testing
Complexity
When Active Record Works Best
When to Consider Alternatives
Comparison with Other Patterns
Pattern
Data
Persistence Logic
Business Logic
Example: Complex Active Record Model
Last updated
Was this helpful?