sourcing()
The primary entry point for creating and retrieving projection/aggregation instances.
Signature
sub sourcing(Sourcing::Projection:U $proj, *%ids) is export
Parameters
| Parameter | Type | Description |
|---|---|---|
$proj | Sourcing::Projection:U | The projection or aggregation type (type object, not instance) |
*%ids | Named arguments | Values for the projection's ID attributes (e.g., :id(42)) |
Returns
A fresh instance of the projection/aggregation with all events from the store applied. Each call creates a new instance — there is no instance caching.
How It Works
- Retrieves all events for the given identity from the store via
get-events-after: -1 - Creates a fresh instance with the provided IDs and events
- Sets
__current-version__to the total number of events minus one - Stores updated cached data via the plugin
- Returns the new instance
Example
# Create a new aggregation instance
my $account = sourcing BankAccount, :account-id(1);
# Create a projection instance
my $view = sourcing AccountView, :account-id(1);
# Multiple IDs
my $order = sourcing OrderLine, :order-id(1), :line-id(3);
Important Notes
Always Creates Fresh Instances
Each call to sourcing() creates a new instance and replays all events. It does not return cached instances. This ensures you always get a clean, consistent snapshot of the event stream.
See Also
- Sourcing::Projection — the role composed into projections
- Sourcing::Aggregation — the marker role for aggregations
- Sourcing::Plugin::Memory — the in-memory event store