acc-cqrs-knowledge
1
总安装量
1
周安装量
#42797
全站排名
安装命令
npx skills add https://github.com/dykyi-roman/awesome-claude-code --skill acc-cqrs-knowledge
Agent 安装分布
opencode
1
claude-code
1
Skill 文档
CQRS Knowledge Base
Quick reference for CQRS architecture patterns and PHP implementation guidelines.
Core Principles
Separation of Concerns
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â APPLICATION â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â WRITE SIDE (Commands) â READ SIDE (Queries) â
âââââââââââââââââââââââââââââââ¼ââââââââââââââââââââââââââââââââ¤
â Command â Handler â Domain â Query â Handler â ReadModel â
â Changes state â Returns data, no side effectsâ
â Uses Domain Model â Can bypass Domain Model â
â Single aggregate per cmd â Can join multiple sources â
âââââââââââââââââââââââââââââââ´ââââââââââââââââââââââââââââââââ
Rule: Commands WRITE, Queries READ. Never mix.
CQRS Components
| Component | Purpose | Returns | Side Effects |
|---|---|---|---|
| Command | Request to change state | void or ID | Yes |
| CommandHandler | Executes command logic | void or ID | Yes |
| Query | Request for data | Data DTO | No |
| QueryHandler | Fetches and transforms data | Data DTO | No |
| CommandBus | Routes commands to handlers | Depends | N/A |
| QueryBus | Routes queries to handlers | Query result | N/A |
Quick Checklists
Command Checklist
- Named as imperative verb + noun (CreateOrder, ConfirmPayment)
- Immutable (readonly class)
- Contains only data needed for operation
- Returns void or created ID (never entities)
- One command = one aggregate affected
- Validated before dispatch
Query Checklist
- Named as Get/Find/List + noun (GetOrderDetails, ListCustomers)
- Immutable (readonly class)
- Contains filtering/pagination params
- Handler has NO side effects
- Can use optimized read models
- Returns DTOs, not entities
Handler Checklist
- Single
execute()or__invoke()method - One handler per command/query
- CommandHandler can dispatch domain events
- QueryHandler never dispatches events
- No cross-aggregate transactions in single handler
Common Violations Quick Reference
| Violation | Where to Look | Severity |
|---|---|---|
| Query with side effects | QueryHandler calling save() | Critical |
| Command returning data | CommandHandler returning entity | Critical |
| Mixed read/write in handler | Handler with both get and save | Critical |
| Business logic in handler | if/switch on domain state | Warning |
| Missing command validation | Command without invariants | Warning |
| Query using write DB | QueryHandler using EntityManager | Info |
PHP 8.5 CQRS Patterns
Command
final readonly class CreateOrderCommand
{
public function __construct(
public CustomerId $customerId,
/** @var array<OrderLineData> */
public array $lines,
public ?string $notes = null
) {
if (empty($lines)) {
throw new InvalidArgumentException('Order must have at least one line');
}
}
}
Command Handler
final readonly class CreateOrderHandler
{
public function __construct(
private OrderRepositoryInterface $orders,
private EventDispatcherInterface $events
) {}
public function __invoke(CreateOrderCommand $command): OrderId
{
$order = Order::create(
id: OrderId::generate(),
customerId: $command->customerId,
lines: $command->lines
);
$this->orders->save($order);
foreach ($order->releaseEvents() as $event) {
$this->events->dispatch($event);
}
return $order->id();
}
}
Query
final readonly class GetOrderDetailsQuery
{
public function __construct(
public OrderId $orderId
) {}
}
Query Handler
final readonly class GetOrderDetailsHandler
{
public function __construct(
private OrderReadModelInterface $readModel
) {}
public function __invoke(GetOrderDetailsQuery $query): ?OrderDetailsDTO
{
return $this->readModel->findById($query->orderId);
}
}
References
For detailed information, load these reference files:
references/command-patterns.mdâ Command structure, naming, validationreferences/query-patterns.mdâ Query structure, read modelsreferences/handler-patterns.mdâ Handler patterns, async/syncreferences/bus-patterns.mdâ Command/Query bus implementationsreferences/antipatterns.mdâ Common violations with detection patterns