AgentSkillsCN

arch-events

针对Python开发,提供事件驱动架构模式,包括领域事件、命令、消息总线、CQRS以及Bootstrap架构。适用于事件溯源的实现、解耦工作流、响应式系统的设计、微服务间的集成,或在事件驱动型设计中做出明智决策。

SKILL.md
--- frontmatter
name: arch-events
description: Event-driven architecture patterns for Python including Domain Events, Commands, Message Bus, CQRS, and Bootstrap. Use for implementing event sourcing, decoupled workflows, reactive systems, microservices integration, or making decisions about event-driven design.
allowed-tools: Read, Grep, Glob

Event-Driven Architecture

Patterns for building reactive, decoupled Python applications using domain events and commands.

Foundation

These patterns build on domain-driven architecture principles (see domain-driven-architecture skill for Repository, Service Layer, Unit of Work, and Aggregates).

Core Patterns

Domain Events

Facts about what happened in the business domain.

  • Immutable dataclasses with past-tense naming (OrderAllocated, OutOfStock)
  • Aggregates collect events during operations
  • Published after transaction commits

Commands

Instructions to perform actions - requests that may fail.

  • Imperative naming (Allocate, CreateBatch)
  • Exactly one handler per command
  • Fail noisily with exceptions

Message Bus

Central routing for commands and events.

  • Routes messages to appropriate handlers
  • Handlers may emit new events (handler chaining)
  • Enables reactive, decoupled systems

CQRS (Command Query Responsibility Segregation)

Separate models for reads and writes.

  • Write model: Full domain logic, transactional consistency
  • Read model: Denormalized views, optimized for queries
  • Event handlers keep read models updated

Bootstrap Pattern

Composition root for dependency wiring.

  • Single initialization point for the application
  • Wire handlers with their dependencies
  • Enables easy testing with fakes

Commands vs Events

AspectCommandEvent
Intent"Please do this""This happened"
NamingImperativePast tense
HandlersExactly oneZero or more
FailureRaises exceptionHandler fails independently

When to Apply

ScenarioPattern
Side effects from domain operationsDomain Events
Decoupled workflowsMessage Bus
Request/response operationsCommands
Query performance at scaleCQRS
Testable dependency managementBootstrap

Event Flow

code
Command → Handler → Aggregate → Collect Events → UoW Commit → Publish Events
                                                                    ↓
                                                              Event Handlers

See reference.md for detailed explanations and examples.md for implementations.