Class ReadModels
- Namespace
- Cratis.Chronicle.ReadModels
- Assembly
- Cratis.Chronicle.dll
Represents an implementation of IReadModels.
public class ReadModels : IReadModels
- Inheritance
-
ReadModels
- Implements
- Inherited Members
- Extension Methods
Constructors
ReadModels(IEventStore, INamingPolicy, IProjections, IReducers, IEventTypes, IJsonSchemaGenerator, IOptions<ChronicleOptions>, JsonSerializerOptions, IReadModelWatcherManager, IReducerObservers, IMaterializedReadModels, ILogger<ReadModels>)
Represents an implementation of IReadModels.
public ReadModels(IEventStore eventStore, INamingPolicy namingPolicy, IProjections projections, IReducers reducers, IEventTypes eventTypes, IJsonSchemaGenerator schemaGenerator, IOptions<ChronicleOptions> options, JsonSerializerOptions jsonSerializerOptions, IReadModelWatcherManager readModelWatcherManager, IReducerObservers reducerObservers, IMaterializedReadModels materializedReadModels, ILogger<ReadModels> logger)
Parameters
eventStoreIEventStoreThe IEventStore to use.
namingPolicyINamingPolicyThe INamingPolicy to use for converting names during serialization.
projectionsIProjectionsProjections to get read models from.
reducersIReducersReducers to get read models from.
eventTypesIEventTypesThe IEventTypes for resolving event types.
schemaGeneratorIJsonSchemaGeneratorSchema generator to use.
optionsIOptions<ChronicleOptions>The IOptions<TOptions> for Chronicle configuration.
jsonSerializerOptionsJsonSerializerOptionsThe JsonSerializerOptions to use for JSON serialization.
readModelWatcherManagerIReadModelWatcherManagerIReadModelWatcherManager for managing watchers.
reducerObserversIReducerObserversIReducerObservers for managing reducer observers.
materializedReadModelsIMaterializedReadModelsThe IMaterializedReadModels for materialized read model operations.
loggerILogger<ReadModels>The ILogger<TCategoryName> for logging.
Properties
Materialized
Gets the IMaterializedReadModels for working with materialized read model instances.
public IMaterializedReadModels Materialized { get; }
Property Value
Methods
DehydrateSession(ReadModelSessionId, Type, ReadModelKey)
Dehydrate a session.
public Task DehydrateSession(ReadModelSessionId sessionId, Type readModelType, ReadModelKey readModelKey)
Parameters
sessionIdReadModelSessionIdThe ReadModelSessionId to dehydrate.
readModelTypeTypeType of read model.
readModelKeyReadModelKeyReadModelKey to dehydrate for.
Returns
- Task
Awaitable task.
GetInstanceById(Type, ReadModelKey, ReadModelSessionId?)
Get a read model instance by its key.
public Task<object> GetInstanceById(Type readModelType, ReadModelKey key, ReadModelSessionId? sessionId = null)
Parameters
readModelTypeTypeThe read model type.
keyReadModelKeyThe ReadModelKey to get instance for.
sessionIdReadModelSessionIdOptional ReadModelSessionId to get for a specific session.
Returns
- Task<object>
The read model instance, or null when no instance exists for the key (never created or removed) — consistently for both projection- and reducer-backed read models.
GetInstanceById<TReadModel>(EventSourceId, EventStreamId)
Folds an instance of a read model declaring EventStreamAttribute<TSource> from one stream.
public Task<TReadModel> GetInstanceById<TReadModel>(EventSourceId eventSourceId, EventStreamId eventStreamId)
Parameters
eventSourceIdEventSourceIdThe owning event source id, also the model key.
eventStreamIdEventStreamIdThe stream id. The default id does not narrow by stream id.
Returns
- Task<TReadModel>
The instance, or null when no matching events created it. This plain read supplies no append guard.
Type Parameters
TReadModelThe read model type.
Exceptions
- DecisionReadRefused
The implementation does not support stream scope.
GetInstanceById<TReadModel>(ReadModelKey, ReadModelSessionId?)
Get a read model instance by its key.
public Task<TReadModel> GetInstanceById<TReadModel>(ReadModelKey key, ReadModelSessionId? sessionId = null)
Parameters
keyReadModelKeyThe ReadModelKey to get instance for.
sessionIdReadModelSessionIdOptional ReadModelSessionId to get for a specific session.
Returns
- Task<TReadModel>
The read model instance, or null when no instance exists for the key (never created or removed) — consistently for both projection- and reducer-backed read models.
Type Parameters
TReadModelThe read model type.
GetInstances<TReadModel>(EventCount?)
Get all instances of a read model by applying all events.
public Task<IEnumerable<TReadModel>> GetInstances<TReadModel>(EventCount? eventCount = null)
Parameters
eventCountEventCountOptional maximum number of events to process. Defaults to Unlimited.
Returns
- Task<IEnumerable<TReadModel>>
Collection of read model instances.
Type Parameters
TReadModelThe read model type.
GetSnapshotsById<TReadModel>(ReadModelKey)
Get snapshots of a read model grouped by CorrelationId by walking through events from the beginning.
public Task<IEnumerable<ReadModelSnapshot<TReadModel>>> GetSnapshotsById<TReadModel>(ReadModelKey readModelKey)
Parameters
readModelKeyReadModelKeyReadModelKey to get snapshots for.
Returns
- Task<IEnumerable<ReadModelSnapshot<TReadModel>>>
Collection of ReadModelSnapshot<TReadModel>.
Type Parameters
TReadModelType of read model.
GetWatcherFor<TReadModel>()
Get the underlying IReadModelWatcher<TReadModel> for a specific read model. Useful when the caller needs explicit control over lifecycle or to await Subscribed before producing events.
public IReadModelWatcher<TReadModel> GetWatcherFor<TReadModel>()
Returns
- IReadModelWatcher<TReadModel>
Type Parameters
TReadModelType of read model to get a watcher for.
Observe<TReadModel>(Expression<Func<TReadModel, bool>>?)
Observes the full collection of a projection- or reducer-backed read model, initially and after changes.
public ISubject<IEnumerable<TReadModel>> Observe<TReadModel>(Expression<Func<TReadModel, bool>>? filter = null)
Parameters
filterExpression<Func<TReadModel, bool>>Optional filter, compiled once and applied client-side after release.
Returns
- ISubject<IEnumerable<TReadModel>>
A subject that replays the latest collection. Its dedicated stream stops when the last subscriber leaves, on completion, or on disposal.
Type Parameters
TReadModelThe read model type.
Remarks
The default implementation refuses collection observation. Existing or custom implementations remain usable through the other members of this interface and must override this method to support collection observation.
Exceptions
- UnknownReadModel
No projection or reducer handles the read model.
- ReadModelCannotBeObserved
The implementation does not support collection observation, or the read model is passive or targets an event sequence.
Register()
Register the read models in the system.
public Task Register()
Returns
- Task
An awaitable task.
Register<TReadModel>()
Register a specific read model type.
public Task Register<TReadModel>()
Returns
- Task
An awaitable task.
Type Parameters
TReadModelThe type of the read model to register.
Remarks
A read model that no projection or reducer in this client maintains yet, but that carries model-bound projection attributes, is registered explicitly: its model-bound projection is registered through Register<TReadModel>(), which makes it behave exactly like a discovered read model from then on - including in the registration pass that runs when the connection is re-established. To register a read model with a projection defined in code rather than by attributes, use Register<TReadModel>(Action<IProjectionBuilderFor<TReadModel>>, ProjectionId?).
Release<TReadModel>(IEnumerable<TReadModel>)
Release (decrypt) compliance- and security-annotated properties in a collection of read model instances.
public Task<IEnumerable<TReadModel>> Release<TReadModel>(IEnumerable<TReadModel> instances)
Parameters
instancesIEnumerable<TReadModel>The collection of instances to decrypt.
Returns
- Task<IEnumerable<TReadModel>>
The decrypted instances.
Type Parameters
TReadModelThe read model type.
Remarks
The subject is derived from each instance individually. See Release<TReadModel>(TReadModel) for details.
Release<TReadModel>(TReadModel)
Release (decrypt) compliance ([PII]) and security ([Encrypted]) annotated properties in a
read model instance by deriving the subject from the instance itself.
public Task<TReadModel> Release<TReadModel>(TReadModel instance)
Parameters
instanceTReadModelThe read model instance to decrypt.
Returns
- Task<TReadModel>
The decrypted instance, or the original when release is not applicable or fails.
Type Parameters
TReadModelThe read model type.
Remarks
The subject is resolved by looking for a property or constructor parameter decorated with SubjectAttribute that has a value,
falling back to a property named Id when the decorated value is null or otherwise not set. If no subject can be derived,
or the read model has no compliance- or security-annotated properties, the original instance is returned unchanged.
A warning is logged when schema metadata exists but no subject can be resolved.
If decryption fails (e.g. the encryption key has been permanently deleted), the original instance is returned and
the failure is logged.
Watch<TReadModel>()
Observe changes for a specific read model.
public IObservable<ReadModelChangeset<TReadModel>> Watch<TReadModel>()
Returns
- IObservable<ReadModelChangeset<TReadModel>>
An observable of ReadModelChangeset<TReadModel>.
Type Parameters
TReadModelType of read model to observe changes for.