Sourcing::Projection
The role automatically composed into all projection classes. Provides event application infrastructure and version tracking.
Attributes
| Attribute | Type | Description |
|---|---|---|
$!__current-version__ | Int | Index of the last event applied during replay. Set to -1 when no events exist, or @events.elems - 1 after replay. |
Methods
multi method new(:@initial-events!, |c)
Creates a new projection instance and applies initial events. Malformed events are logged and skipped (resilient projections).
| Parameter | Description |
|---|---|
:@initial-events | Events to apply during construction |
|c | Additional arguments passed to bless |
Returns: New projection instance with events applied.
method apply(Event $e)
Abstract method that must be implemented by the projection. Called for each event during replay. Projections typically define multiple multi candidates, one per event type.
multi method apply(OrderPlaced $e) {
$!status = 'placed';
$!total = $e.total;
}
multi method apply(OrderShipped $e) {
$!status = 'shipped';
}
How It's Used
You never use this role directly. It's automatically composed when you declare a projection:
projection MyView {
has Int $.id is projection-id;
# Sourcing::Projection is automatically composed here
}
See Also
- Projections Concept — when and why to use projections
- Sourcing::Aggregation — the write-side role
- Metaclasses — how the
projectionkeyword works