Ejemplo n.º 1
0
        public void Should_not_add_event_to_uncommitted_events_and_increase_version_when_raised()
        {
            var event1 = new MyEvent();
            var event2 = new MyEvent();

            var sut = new DO(Guid.NewGuid(), 10);

            IAggregate aggregate = sut;

            aggregate.ApplyEvent(new Envelope <IEvent>(event1));
            aggregate.ApplyEvent(new Envelope <IEvent>(event2));

            Assert.Equal(12, sut.Version);
            Assert.Equal(0, sut.GetUncomittedEvents().Count);
        }
 public static void LoadsFromHistory(this IAggregate aggregate, IEnumerable <IEvent> history)
 {
     foreach (var @event in history)
     {
         aggregate.ApplyEvent(@event);
     }
 }
Ejemplo n.º 3
0
 public static void ApplyEvents(this IAggregate aggregate, params DomainEvent[] events)
 {
     foreach (var e in events)
     {
         aggregate.ApplyEvent(e);
     }
 }
        public async Task LoadAsync(IAggregate domainObject, long?expectedVersion = null)
        {
            var streamName = nameResolver.GetStreamName(domainObject.GetType(), domainObject.Id);

            var events = await eventStore.GetEventsAsync(streamName);

            if (events.Count == 0)
            {
                throw new DomainObjectNotFoundException(domainObject.Id.ToString(), domainObject.GetType());
            }

            foreach (var storedEvent in events)
            {
                var envelope = ParseKnownCommand(storedEvent);

                if (envelope != null)
                {
                    domainObject.ApplyEvent(envelope);
                }
            }

            if (expectedVersion != null && domainObject.Version != expectedVersion.Value)
            {
                throw new DomainObjectVersionException(domainObject.Id.ToString(), domainObject.GetType(), domainObject.Version, expectedVersion.Value);
            }
        }
Ejemplo n.º 5
0
 private static void ApplyEventsToAggregate(int versionToLoad, IEventStream stream, IAggregate aggregate)
 {
     if (versionToLoad == 0 || aggregate.Version < versionToLoad)
     {
         foreach (var @event in stream.CommittedEvents.Select(x => x.Body))
         {
             aggregate.ApplyEvent(@event);
         }
     }
 }
Ejemplo n.º 6
0
        public EventSourcedActor(IConstructAggregates aggregateConstructor,
                                 ISnapshotsPersistencePolicy policy,
                                 IPublisher publisher)
        {
            PersistenceId         = Self.Path.Name;
            SnapshotsPolicy       = policy;
            _aggregateConstructor = aggregateConstructor;
            Publisher             = publisher;
            Id      = AggregateActorName.Parse <T>(Self.Path.Name).Id;
            State   = (AggregateBase)aggregateConstructor.Build(typeof(T), Id, null);
            Monitor = new ActorMonitor(Context, typeof(T).Name);
            Command <GracefullShutdownRequest>(req =>
            {
                Monitor.IncrementMessagesReceived();
                DeleteSnapshots(SnapshotsPolicy.GetSnapshotsToDelete());
                Become(Terminating);
            });

            Command <CheckHealth>(s => Sender.Tell(new HealthStatus(s.Payload)));

            Command <SaveSnapshotSuccess>(s =>
            {
                NotifyWatchers(s);
                SnapshotsPolicy.MarkSnapshotSaved(s.Metadata);
            });

            Command <NotifyOnPersistenceEvents>(c =>
            {
                var waiter = c.Waiter ?? Sender;
                if (IsRecoveryFinished)
                {
                    waiter.Tell(RecoveryCompleted.Instance);
                }

                _persistenceWaiters.Add(waiter);
            });

            Recover <DomainEvent>(e =>
            {
                State.ApplyEvent(e);
                SnapshotsPolicy.MarkActivity(e.CreatedTime);
            });

            Recover <SnapshotOffer>(offer =>
            {
                SnapshotsPolicy.MarkSnapshotApplied(offer.Metadata);
                State = _aggregateConstructor.Build(typeof(T), Id, (IMemento)offer.Snapshot);
            });

            Recover <RecoveryCompleted>(message =>
            {
                _log.Debug("Recovery for actor {Id} is completed", PersistenceId);
                NotifyWatchers(message);
            });
        }
 private static void ApplyEventsToAggregate(int versionToLoad, IEventStream stream, IAggregate aggregate)
 {
     if (versionToLoad == 0 || aggregate.Version < versionToLoad)
     {
         // This is where we need to filter out any null events.
         foreach (var @event in stream.CommittedEvents.Where(e => e != null).Select(x => x.Body))
         {
             aggregate.ApplyEvent(@event);
         }
     }
 }
Ejemplo n.º 8
0
 private static void ApplyEventsToAggregate(int versionToLoad, IEventStream stream, IAggregate aggregate)
 {
     if (versionToLoad != 0 && aggregate.Version >= versionToLoad)
     {
         return;
     }
     foreach (object @event in stream.CommittedEvents.Select(x => x.Body))
     {
         aggregate.ApplyEvent(@event);
     }
 }
Ejemplo n.º 9
0
        public IAggregate TryGetById(string streamName)
        {
            EnsureConnected();

            IAggregate aggregate = BuildAggregate(streamName);

            StreamEventsSlice currentSlice;
            var nextSliceStart = 0;

            do
            {
                currentSlice   = _eventStoreConnection.ReadStreamEventsForward(streamName, nextSliceStart, ReadPageSize, true);
                nextSliceStart = currentSlice.NextEventNumber;

                foreach (var evnt in currentSlice.Events)
                {
                    aggregate.ApplyEvent(DeserializeEvent(evnt.Event.Metadata, evnt.Event.Data));
                }
            } while (!currentSlice.IsEndOfStream);


            return(aggregate);
        }
Ejemplo n.º 10
0
 private void ApplyEvent <TEvent>(IAggregate aggregate, TEvent @event)
 {
     aggregate.ApplyEvent(@event);
 }