Event Sourced Aggregate
ASecurityBuild event-sourced aggregates in Protean where state is derived by replaying domain events instead of stored as snapshots. Covers the @apply decorator for event application, raising events from business methods, state reconstruction via from_events(), event-sourced repositories, version tracking for optimistic concurrency, and fact events. Use when the user asks to 'create an event-sourced aggregate', 'add event sourcing', 'use event sourcing', 'store events instead of state', 'add an @apply...
- 46 stars
- 0 votes
- 0 copies
- 1 view
- Added September 20, 2026
Security analysis
100/100Pro scans all 9 files and shows the line behind each finding
npx -y skills add proteanhq/protean --skill event-sourced-aggregate --agent claude-codeAre you the author of Event Sourced Aggregate?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/proteanhq-event-sourced-aggregate)---
name: event-sourced-aggregate
description: "Build event-sourced aggregates in Protean where state is derived by replaying domain events instead of stored as snapshots. Covers the @apply decorator for event application, raising events from business methods, state reconstruction via from_events(), event-sourced repositories, version tracking for optimistic concurrency, and fact events. Use when the user asks to 'create an event-sourced aggregate', 'add event sourcing', 'use event sourcing', 'store events instead of state', 'add an @apply method', 'enable event_sourced', or when they need audit trails, temporal queries, or complete state history for an aggregate."
license: Apache-2.0
compatibility: Requires Python 3.11+, protean framework
metadata:
author: proteanhq
version: "0.1"
category: element
diagnostic_codes:
- ES_AGGREGATE_NO_EVENTS
- ES_EVENT_MISSING_APPLY
---
# Event-Sourced Aggregate
## Basic structure
An event-sourced aggregate stores events instead of current state. State is reconstructed by replaying events through `@apply` methods. Business methods validate and call `raise_()`, which automatically invokes the matching `@apply` handler to mutate state:
```python
from protean import Domain
from protean.core.aggregate import apply
from protean.fields import String, Identifier
domain = Domain()
@domain.event(part_of="Account")
class AccountOpened:
account_id: Identifier(required=True)
owner_name: String(required=True)
@domain.event(part_of="Account")
class AccountClosed:
account_id: Identifier(required=True)
@domain.aggregate(event_sourced=True)
class Account:
account_id: Identifier(identifier=True)
owner_name: String(required=True)
status: String(default="ACTIVE")
@classmethod
def open(cls, account_id, owner_name):
account = cls(account_id=account_id, owner_name=owner_name)
account.raise_(AccountOpened(
account_id=account_id, owner_name=owner_name
))
return account
def close(self):
if self.status != "ACTIVE":
raise ValueError("Account is not active")
self.raise_(AccountClosed(account_id=self.account_id))
@apply
def opened(self, event: AccountOpened):
self.account_id = event.account_id
self.owner_name = event.owner_name
self.status = "ACTIVE"
@apply
def closed(self, event: AccountClosed):
self.status = "CLOSED"
```
## Key rules
1. **Enable with `event_sourced=True`**: Pass to the `@domain.aggregate` decorator
2. **`@apply` is the single source of truth for state mutations**: `raise_()` automatically invokes the matching `@apply` handler to mutate state. Business methods should validate preconditions and call `raise_()`. They never mutate state directly
3. **Every event must have a corresponding `@apply` handler**: When `raise_()` is called, it looks up and invokes the `@apply` handler for that event type. A missing handler raises `IncorrectUsageError`
4. **`@apply` runs on both live and replay paths**: The same `@apply` handler runs when `raise_()` is called during normal operations AND when replaying events from the event store. This guarantees identical state regardless of path
5. **Each `@apply` method handles exactly one event type**: Use a type annotation on the event parameter: `def opened(self, event: AccountOpened)`
6. **Use factory classmethods for creation**: Create instance, raise initial event, return instance. This ensures the creation event is always the first event in the aggregate's stream
7. **Import `apply` from `protean.core.aggregate`**: `from protean import apply` also works and is the same decorator; this skill uses the `protean.core.aggregate` path throughout
8. **Events must be registered with `part_of`**: Associate each event with its aggregate: `@domain.event(part_of="Account")`
9. **Version auto-increments with each event**: Each raised domain event increments `_version`, which drives optimistic concurrency control. Fact-event snapshots (rule 11) do not increment `_version`
10. **ES repository is selected automatically**: `domain.repository_for(Account)` returns an event-sourced repository when the aggregate has `event_sourced=True`
11. **Fact events auto-generate state snapshots**: Use `@domain.aggregate(event_sourced=True, fact_events=True)` to auto-publish complete state after each persist
12. **First event's `@apply` must set ALL fields**: `from_events()` creates a blank aggregate and applies all events through `@apply`, so the first event's handler must establish all state including identity
13. **A failed handler writes no events**: Events added to the repository inside a command handler or a `with UnitOfWork():` block reach the event store only when the unit of work commits. If the handler raises after `repo.add(account)`, the unit of work rolls back and none of the new events reach the account's stream. A new account's stream stays empty; an existing account's stream keeps the events it already had. The `account` object in memory is not reset: it still holds the raised events in `_events` and the state its `@apply` handlers set. Do not reuse it.
14. **`reserved=[...]` names removed fields**: `@domain.aggregate(event_sourced=True, reserved=["nickname"])` marks `nickname` as a field that once existed. Declaring a live field with a reserved name fails at registration with `IncorrectUsageError`, message "Field(s) ['nickname'] on aggregate \`Account\` reuse a reserved name; a reserved name can never be declared again as a field". During replay (`from_events()` or `repo.get()`), an `@apply` handler for an old event can still run `self.nickname = event.nickname`. The assignment is dropped without an error, and the rebuilt aggregate has no `nickname` attribute. The same assignment on the live `raise_()` path raises a Pydantic `pydantic_core.ValidationError`, not `protean.exceptions.ValidationError`.
## The @apply pattern
Using `Account.close()` from Basic structure:
When `close()` is called:
1. The business method validates preconditions
2. `raise_()` increments `_version`, appends the event, then invokes the `@apply` handler (wrapped in invariant checks)
3. The `@apply` handler mutates state (`self.status = "CLOSED"`)
4. The event is persisted to the event store when the repository saves
When the aggregate is loaded later (via `from_events()` or `repo.get()`):
1. All events are read from the event store
2. Each event is replayed through its `@apply` method
3. The aggregate arrives at its current state
An `@apply` handler changes a `HasMany` collection with the aggregate's `add_<field>()` and `remove_<field>()` helpers. Replay then rebuilds the child entities as well.
## Factory classmethod pattern
Always use a factory classmethod to create new event-sourced aggregates. Here `User.register()` is the factory:
```python
@domain.event(part_of="User")
class UserRegistered:
user_id: Identifier(required=True)
name: String(required=True)
email: String(required=True)
@domain.aggregate(event_sourced=True)
class User:
user_id: Identifier(identifier=True)
name: String(required=True)
email: String(required=True)
@classmethod
def register(cls, user_id, name, email):
user = cls(user_id=user_id, name=name, email=email)
user.raise_(UserRegistered(user_id=user_id, name=name, email=email))
return user
@apply
def registered(self, event: UserRegistered):
self.user_id = event.user_id
self.name = event.name
self.email = event.email
# Usage
domain.init(traverse=False)
with domain.domain_context():
user = User.register(user_id="U-001", name="Alice", email="alice@example.com")
```
### Alternative: `_create_new()` for lean creation
When not all required fields can be satisfied at construction time, use `_create_new()`. It creates a blank aggregate with only identity, and lets the creation event's `@apply` handler establish all state:
```python
@classmethod
def register(cls, user_id, name, email):
user = cls._create_new(user_id=user_id) # Only identity required
user.raise_(UserRegistered(user_id=user_id, name=name, email=email))
return user
```
With `_create_new()`, the `@apply` handler for `UserRegistered` must set ALL fields. This is the preferred pattern when the aggregate has many required fields.
## State reconstruction
Event-sourced aggregates can be reconstructed from their event history:
```python
with domain.domain_context():
events = user._events # the events raised so far
# Build aggregate from events
rebuilt = User.from_events(events)
assert rebuilt.email == user.email
# Internally:
# 1. Creates a blank aggregate via _create_for_reconstitution()
# 2. Applies ALL events sequentially through their @apply methods
# 3. The first event's @apply handler must set ALL fields including identity
```
The repository handles this automatically when loading:
```python
with domain.domain_context():
repo = domain.repository_for(User)
repo.add(user) # Writes the raised events to the event store
user = repo.get(user.user_id) # Reconstructs from event store
```
## Event-sourced repository
`repository_for()` returns an event-sourced repository, which persists events instead of state ([State reconstruction](#state-reconstruction) shows `add()` and `get()`). For custom queries, define an explicit ES repository by subclassing `BaseEventSourcedRepository` and registering it against the aggregate:
```python
from protean.core.event_sourced_repository import BaseEventSourcedRepository
class AccountRepository(BaseEventSourcedRepository):
pass # Default behavior handles add/get
domain.register(AccountRepository, part_of=Account)
```
See [Event-Sourced Repository](references/event-sourced-repository.md) for details.
## Fact events
Enable fact events to auto-generate state snapshots after each persist:
```python
@domain.aggregate(event_sourced=True, fact_events=True)
class Product:
name: String(required=True)
price: Decimal(precision=19, scale=4, required=True)
...
```
Each time the aggregate is saved, Protean auto-generates a `ProductFactEvent` containing the complete state. Downstream consumers can subscribe to these instead of tracking individual delta events.
Use fact events when:
- External systems need complete state snapshots
- Migrating from traditional to event-sourced architecture
- Building projections that need full state rather than deltas
See [Snapshots and Fact Events](references/snapshots-and-fact-events.md) for details.
## When event sourcing fits
Choose event sourcing when an aggregate meets **2 or more** of these criteria:
| Criterion | Indicator |
|-----------|-----------|
| **Audit trail** | Regulatory compliance, financial transactions, complete traceability |
| **Temporal queries** | "What was the state at time T?", historical reporting |
| **Complex state transitions** | Multi-step workflows, intricate business rules |
| **Event-driven integration** | Other bounded contexts consume events as primary data |
| **Team readiness** | Team understands event modeling and can operate event stores |
You can mix patterns per-aggregate within the same domain:
```python
@domain.aggregate # Standard: state stored directly
class Product: ...
@domain.aggregate(event_sourced=True) # Event sourced: state from events
class Order: ...
```
## Common mistakes
### Mutating state directly in business methods
```python
# fragment
# WRONG: state mutated in business method AND in @apply (double mutation)
def close(self):
self.status = "CLOSED" # Don't do this
self.raise_(AccountClosed(account_id=self.account_id))
@apply
def closed(self, event: AccountClosed):
self.status = "CLOSED" # This also runs via raise_()!
```
Business methods only validate and call `raise_()`, as `Account.close()` does in Basic structure.
### Missing @apply handler for an event
Every event type raised by the aggregate must have a corresponding `@apply` method. Missing one raises `IncorrectUsageError` at runtime (`No @apply handler registered for event ...`) because `raise_()` invokes `@apply` automatically. `check` reports this ahead of time as `ES_EVENT_MISSING_APPLY`.
### Event-sourced aggregate with no events
An `event_sourced=True` aggregate that raises no domain events has no state history and cannot be rebuilt by replay. `check` reports this as `ES_AGGREGATE_NO_EVENTS`: declare at least one event with `part_of=<Aggregate>` and raise it from the aggregate's behaviour, or drop `event_sourced=True` if the aggregate is not meant to be event-sourced.
### Forgetting to raise initial event in factory
```python
# fragment
# WRONG: no creation event recorded
@classmethod
def open(cls, account_id, owner_name):
return cls(account_id=account_id, owner_name=owner_name)
```
Always `raise_()` an event in the factory classmethod.
### Using standard repository with ES aggregate
`repository_for()` returns the event-sourced repository automatically for an ES aggregate. For custom query methods, subclass `BaseEventSourcedRepository` and register it, and `repository_for()` returns an instance of that subclass. A standard `@domain.repository` registered for an ES aggregate raises `IncorrectUsageError`.
See [Anti-patterns](references/anti-patterns.md) for more.
## Detailed references
### Core Concepts
- [The @apply Decorator](references/apply-decorator.md): Mechanics, validation rules, projection maps
- [Event-Sourced Repository](references/event-sourced-repository.md): Persistence, loading, UnitOfWork integration
- [Snapshots and Fact Events](references/snapshots-and-fact-events.md): Performance optimization, state snapshots
- [Anti-patterns](references/anti-patterns.md): Common mistakes and how to avoid them
### Complete Examples
- [Basic ES Aggregate](assets/es_aggregate_basic.py): User lifecycle with registration, activation, deactivation
- [ES Aggregate with Entities](assets/es_aggregate_with_entities.py): Order with line items and value objects
- [ES Aggregate Command Flow](assets/es_aggregate_command_flow.py): Full command → handler → aggregate → event flow
- [ES Aggregate with Fact Events](assets/es_aggregate_fact_events.py): Auto-generated state snapshots and mixed patterns
### Related Skills
- `aggregate`: Base aggregate structure, fields, invariants, associations
- `event`: Event definition, naming conventions, event fields
- `event-handler`: Event handler structure, @handle decorator
- `command-handler`: Command handler processing
- `repository`: Standard repository (contrast with ES repository)
## Verify your work
- [Verify with check](../../references/verify-with-check.md): run `check` and resolve what it reports
Files in this skill
- SKILL.md
- assets/es_aggregate_basic.py
- assets/es_aggregate_command_flow.py
- assets/es_aggregate_fact_events.py
- assets/es_aggregate_with_entities.py
- references/anti-patterns.md
- references/apply-decorator.md
- references/event-sourced-repository.md
- references/snapshots-and-fact-events.md
Attribution
Related skills
View all in business →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.