Cqrs Implementation

作者 wshobson46891e7e60da無授權條款收錄於 2026年10月8日更新於 2026年10月8日

Implement Command Query Responsibility Segregation for scalable architectures. Use when separating read and write models, optimizing query performance, or building event-sourced systems.

AI 產生的概覽

指導實作 CQRS,分離命令與查詢模型,並在參考檔案中提供範本。

功能
此技能為實作命令查詢職責分離(CQRS)架構提供指引。它說明命令、命令處理器、事件、查詢、查詢處理器與投影器等核心概念,並列出最佳實務與常見誤區。它指向一個參考檔案,其中包含範本庫與詳細範例。
適用情境
適用於分離讀寫模型、獨立擴充讀取作業、建構事件溯源系統,或最佳化複雜查詢與報表情境。它著重於設計與實作指引,而非直接產出程式碼成品。
執行需求
不需要指令碼或工具,僅為說明性內容。如需具體範本,建議閱讀隨附的參考檔案 references/details.md。

CQRS Implementation

Comprehensive guide to implementing CQRS (Command Query Responsibility Segregation) patterns.

When to Use This Skill

  • Separating read and write concerns
  • Scaling reads independently from writes
  • Building event-sourced systems
  • Optimizing complex query scenarios
  • Different read/write data models needed
  • High-performance reporting requirements

Core Concepts

1. CQRS Architecture

                    ┌─────────────┐                    │   Client    │                    └──────┬──────┘                           │              ┌────────────┴────────────┐              │                         │              ▼                         ▼       ┌─────────────┐          ┌─────────────┐       │  Commands   │          │   Queries   │       │    API      │          │    API      │       └──────┬──────┘          └──────┬──────┘              │                         │              ▼                         ▼       ┌─────────────┐          ┌─────────────┐       │  Command    │          │   Query     │       │  Handlers   │          │  Handlers   │       └──────┬──────┘          └──────┬──────┘              │                         │              ▼                         ▼       ┌─────────────┐          ┌─────────────┐       │   Write     │─────────►│    Read     │       │   Model     │  Events  │   Model     │       └─────────────┘          └─────────────┘

2. Key Components

ComponentResponsibility
CommandIntent to change state
Command HandlerValidates and executes commands
EventRecord of state change
QueryRequest for data
Query HandlerRetrieves data from read model
ProjectorUpdates read model from events

Templates and detailed worked examples

Full template library and detailed worked examples live in references/details.md. Read that file when you need the concrete templates.

Best Practices

Do's

  • Separate command and query models - Different needs
  • Use eventual consistency - Accept propagation delay
  • Validate in command handlers - Before state change
  • Denormalize read models - Optimize for queries
  • Version your events - For schema evolution

Don'ts

  • Don't query in commands - Use only for writes
  • Don't couple read/write schemas - Independent evolution
  • Don't over-engineer - Start simple
  • Don't ignore consistency SLAs - Define acceptable lag

來源與署名

來源:wshobson/agents位於plugins/backend-development/skills/cqrs-implementation提交46891e7

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架