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:

  1. Validate title/body length
  2. Sanitize filename
  3. Write file atomically
  4. Return success metadata (path, timestamp)