Follow this guide to create a Single Stream Projection in Marten. This projection creates a single document for a single event stream (aggregate instance).
- •
Define the Projection Class
- •Create a
classinsrc/BookStore.ApiService/Projections/ - •Naming:
{Resource}Projection(e.g.,AuthorDetailsProjection) - •Base Class:
SingleStreamProjection<T>matches the standard pattern for explicit single stream aggregation. - •Template:
templates/Projection.cs
- •Create a
- •
Configure in Marten
- •Open
src/BookStore.ApiService/Infrastructure/Extensions/MartenConfigurationExtensions.cs - •Register the projection in
RegisterProjections:csharpoptions.Projections.Add<AuthorDetailsProjection>(ProjectionLifecycle.Async);
- •Note:
Asyncis recommended for performance.Inlinehandles consistency but impacts write performance.
- •Open
- •
Indexing (Optional)
- •In
ConfigureIndexes:csharpoptions.Schema.For<AuthorDetailsProjection>().Index(x => x.Name);
- •In
- •
Querying
- •Query this projection by ID (matching the stream ID) or other fields.
- •Example:
session.LoadAsync<AuthorDetailsProjection>(streamId)
Related Skills
- •
/scaffold-aggregate: Create the events first. - •
/scaffold-read: Create an endpoint to query this projection.