Class ProjectionEventHandlerBase<TProjection, TKey, TEvent>
Provides the base implementation for strongly-typed projection event handlers in the event sourcing system. This abstract class defines the contract for implementing domain-specific projection logic while maintaining type safety and enabling polymorphic behavior through the projection handling infrastructure.
public abstract class ProjectionEventHandlerBase<TProjection, TKey, TEvent> where TProjection : IProjection<TKey> where TKey : notnull
Type Parameters
TProjectionThe type of projection being maintained, must implement IProjection<TKey>.
TKeyThe type of key used to identify projections and aggregate roots.
TEventThe specific event type this handler processes.
- Inheritance
-
ProjectionEventHandlerBase<TProjection, TKey, TEvent>
- Inherited Members
- Extension Methods
Remarks
Derived classes must implement the projection resolution and event application logic. This base class serves as the foundation for the Command Query Responsibility Segregation (CQRS) pattern implementation, enabling separate read models optimized for specific query scenarios.
Methods
ApplyAsync(TEvent, TProjection, CancellationToken)
Asynchronously applies the specified event to a projection instance, updating the projection's state to reflect the business changes represented by the event.
public abstract Task<TProjection> ApplyAsync(TEvent evt, TProjection projection, CancellationToken cancellationToken)
Parameters
evtTEventThe event containing the state changes to apply to the projection.
projectionTProjectionThe projection instance to be updated with the event data.
cancellationTokenCancellationTokenToken to monitor for cancellation requests during event application.
Returns
- Task<TProjection>
A task that represents the asynchronous operation, containing the updated projection instance.
Remarks
This method encapsulates the domain-specific logic for transforming events into projection updates. Implementations should ensure idempotency where possible and handle concurrent modifications appropriately. The returned projection should reflect all changes from the applied event.
ResolveMatchingProjectionsAsync(TEvent, CancellationToken)
Asynchronously resolves the collection of projections that should be updated in response to the given event. This method determines which specific projection instances need to be modified based on the event content and business logic requirements.
public abstract Task<IEnumerable<TProjection>> ResolveMatchingProjectionsAsync(TEvent evt, CancellationToken cancellationToken)
Parameters
evtTEventThe event that triggered the projection update process.
cancellationTokenCancellationTokenToken to monitor for cancellation requests during projection resolution.
Returns
- Task<IEnumerable<TProjection>>
A task that represents the asynchronous operation, containing a collection of projections that should be updated in response to the event.
Remarks
Implementations should consider performance implications when resolving projections, potentially using caching or optimized queries to minimize database roundtrips. The method may return an empty collection if no projections match the event criteria.