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

eventStore IEventStore

The IEventStore to use.

namingPolicy INamingPolicy

The INamingPolicy to use for converting names during serialization.

projections IProjections

Projections to get read models from.

reducers IReducers

Reducers to get read models from.

eventTypes IEventTypes

The IEventTypes for resolving event types.

schemaGenerator IJsonSchemaGenerator

Schema generator to use.

options IOptions<ChronicleOptions>

The IOptions<TOptions> for Chronicle configuration.

jsonSerializerOptions JsonSerializerOptions

The JsonSerializerOptions to use for JSON serialization.

readModelWatcherManager IReadModelWatcherManager

IReadModelWatcherManager for managing watchers.

reducerObservers IReducerObservers

IReducerObservers for managing reducer observers.

materializedReadModels IMaterializedReadModels

The IMaterializedReadModels for materialized read model operations.

logger ILogger<ReadModels>

The ILogger<TCategoryName> for logging.

Properties

Materialized

Gets the IMaterializedReadModels for working with materialized read model instances.

public IMaterializedReadModels Materialized { get; }

Property Value

IMaterializedReadModels

Methods

DehydrateSession(ReadModelSessionId, Type, ReadModelKey)

Dehydrate a session.

public Task DehydrateSession(ReadModelSessionId sessionId, Type readModelType, ReadModelKey readModelKey)

Parameters

sessionId ReadModelSessionId

The ReadModelSessionId to dehydrate.

readModelType Type

Type of read model.

readModelKey ReadModelKey

ReadModelKey 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

readModelType Type

The read model type.

key ReadModelKey

The ReadModelKey to get instance for.

sessionId ReadModelSessionId

Optional 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

eventSourceId EventSourceId

The owning event source id, also the model key.

eventStreamId EventStreamId

The 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

TReadModel

The 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

key ReadModelKey

The ReadModelKey to get instance for.

sessionId ReadModelSessionId

Optional 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

TReadModel

The 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

eventCount EventCount

Optional maximum number of events to process. Defaults to Unlimited.

Returns

Task<IEnumerable<TReadModel>>

Collection of read model instances.

Type Parameters

TReadModel

The 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

readModelKey ReadModelKey

ReadModelKey to get snapshots for.

Returns

Task<IEnumerable<ReadModelSnapshot<TReadModel>>>

Collection of ReadModelSnapshot<TReadModel>.

Type Parameters

TReadModel

Type 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>

The IReadModelWatcher<TReadModel>.

Type Parameters

TReadModel

Type 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

filter Expression<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

TReadModel

The 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

TReadModel

The 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

instances IEnumerable<TReadModel>

The collection of instances to decrypt.

Returns

Task<IEnumerable<TReadModel>>

The decrypted instances.

Type Parameters

TReadModel

The 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

instance TReadModel

The read model instance to decrypt.

Returns

Task<TReadModel>

The decrypted instance, or the original when release is not applicable or fails.

Type Parameters

TReadModel

The 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

TReadModel

Type of read model to observe changes for.