05 - Commands and State Management
Commands should be small, validated, and testable.
Managed app state
Use shared state for app-wide services (DB pool, config, cache).
Rust pattern:
use std::sync::Mutex;
struct AppState {
counter: Mutex<u64>,
}
Command design checklist
- Validate all inputs
- Return
Result<T, E> - Avoid blocking the main thread
- Keep side effects explicit
Example scenario
A save_note command should:
- Validate title/body length
- Sanitize filename
- Write file atomically
- Return success metadata (path, timestamp)