/// <summary> /// Loads the aggregate using the latest snapshot and then applies all events /// in the event stream from the stream revision that the snapshot was taken. /// </summary> /// <typeparam name="T">Snapshot type</typeparam> /// <param name="aggregate">The aggregate.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public async Task LoadFromLatestSnapshotAsync <T>(IAggregateWithSnapshot <T> aggregate) where T : class { AggregateWithSnapshot <T> baseAggregate = aggregate as AggregateWithSnapshot <T>; if (baseAggregate == null) { string exceptionMessage = string.Format(CultureInfo.InvariantCulture, "The supplied Aggregate did not inherit from {0}.", typeof(Aggregate).FullName); throw new EventSourcingException(exceptionMessage); } // Load from the snapshot. EventStoreSnapshot eventStoreSnapshot = await this.eventStoreProvider.ReadSnapshotAsync(aggregate.SnapshotStreamId).ConfigureAwait(false); baseAggregate.Load(eventStoreSnapshot); // Now load all events since the snapshot was taken. IEventStoreStream eventStoreStream = await this.eventStoreProvider.ReadEventsAsync( aggregate.EventStreamId, eventStoreSnapshot.StreamRevision, ExpectedStreamRevision.End).ConfigureAwait(false); baseAggregate.Load(eventStoreStream); }
public async Task LoadAsync(IAggregate aggregate) { Aggregate baseAggregate = aggregate as Aggregate; if (baseAggregate == null) { string exceptionMessage = string.Format(CultureInfo.InvariantCulture, "The supplied Aggregate did not inherit from {0}.", typeof(Aggregate).FullName); throw new EventSourcingException(exceptionMessage); } IEventStoreStream eventStoreStream = await this.eventStoreProvider.ReadEventsAsync(aggregate.EventStreamId).ConfigureAwait(false); if (eventStoreStream.Count < 1) { string exceptionMessage = string.Format(CultureInfo.InvariantCulture, "No events could be found in the event store for Event Stream ID '{0}'.", aggregate.EventStreamId); throw new EventSourcingException(exceptionMessage); } baseAggregate.Load(eventStoreStream); }
protected async Task Given_A_Stream_Of_10_Events_When_Reading_Events_From_Three_To_Nine_Then_Six_Events_Should_Be_Returned(IEventStoreProvider eventStoreProvider) { // Arrange string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString(); // Act for (int i = 0; i < 10; i++) { Guid eventId = Guid.NewGuid(); EventStoreMessage eventStoreMessage = new EventStoreMessage(eventId, new object()); eventStoreProvider.AppendEvents(eventStreamId, eventStoreMessage); } await eventStoreProvider.CommitEventsAsync(eventStreamId, ExpectedStreamRevision.New).ConfigureAwait(false); // Assert IEventStoreStream eventStoreStream = await eventStoreProvider.ReadEventsAsync(eventStreamId, 3, 9).ConfigureAwait(false); Assert.Equal(6, eventStoreStream.Count); }
public async Task <bool> LoadIfExistsAsync(IAggregate aggregate) { Aggregate baseAggregate = aggregate as Aggregate; if (baseAggregate == null) { string exceptionMessage = string.Format(CultureInfo.InvariantCulture, "The supplied Aggregate did not inherit from {0}.", typeof(Aggregate).FullName); throw new EventSourcingException(exceptionMessage); } bool streamExists = await this.eventStoreProvider.StreamExistsAsync(aggregate.EventStreamId).ConfigureAwait(false); if (!streamExists) { return(false); } IEventStoreStream eventStoreStream = await this.eventStoreProvider.ReadEventsAsync(aggregate.EventStreamId).ConfigureAwait(false); baseAggregate.Load(eventStoreStream); return(true); }
public static void SubscribeToEventStream(this IActor actor, IEventStoreStream eventStoreStream, bool closeSubscriptionOnDispose = false) { var eventStoreBus = actor.GetConnectedBus <IEventStoreBus>(); eventStoreBus.SubscribeToEventStream(eventStoreStream, actor.OnMessageReceived, closeSubscriptionOnDispose); }