Exemple #1
0
        protected virtual TAggregateRoot CreateAggregate <TAggregateRoot>(Guid id)
            where TAggregateRoot : IAggregateRoot <TAuthenticationToken>
        {
            var aggregate = AggregateFactory.CreateAggregate <TAggregateRoot>(id);

            return(aggregate);
        }
Exemple #2
0
        private async Task <T> LoadAggregateAsync <T>(Guid id) where T : AggregateRoot
        {
            var aggregate = AggregateFactory.CreateAggregate <T>();

            var events = _eventStore.Get(id, -1);

            if (!events.Any())
            {
                try
                {
                    var @event = aggregate.ConstructInitialCreateEvent(id);
                    @event.Version   = 1;
                    @event.TimeStamp = DateTimeOffset.UtcNow;
                    await _eventStore.SaveAsync(@event);

                    events = new[] { @event };
                }
                catch (System.Exception)
                {
                    throw new AggregateNotFoundException(id);
                }
            }
            aggregate.LoadFromHistory(events);
            return(aggregate);
        }
Exemple #3
0
        protected virtual TAggregateRoot LoadAggregate <TAggregateRoot>(Guid id, IList <IEvent <TAuthenticationToken> > events = null)
            where TAggregateRoot : IAggregateRoot <TAuthenticationToken>
        {
            var aggregate = AggregateFactory.CreateAggregate <TAggregateRoot>(id);

            IList <IEvent <TAuthenticationToken> > theseEvents = events ?? EventStore.Get <TAggregateRoot>(id).ToList();

            if (!theseEvents.Any())
            {
                throw new AggregateNotFoundException <TAggregateRoot, TAuthenticationToken>(id);
            }

            var duplicatedEvents =
                theseEvents.GroupBy(x => x.Version)
                .Select(x => new { Version = x.Key, Total = x.Count() })
                .FirstOrDefault(x => x.Total > 1);

            if (duplicatedEvents != null)
            {
                throw new DuplicateEventException <TAggregateRoot, TAuthenticationToken>(id, duplicatedEvents.Version);
            }

            aggregate.LoadFromHistory(theseEvents);
            return(aggregate);
        }
Exemple #4
0
        /// <summary>
        /// Gets the aggregate.
        /// </summary>
        public T Get <T>(Guid aggregateId) where T : AggregateRoot
        {
            // Check the cache to see if the aggregate is already in memory.
            var snapshot = _cache.Get(aggregateId);

            if (snapshot != null)
            {
                return((T)snapshot);
            }

            // If it is not in the cache then load the aggregate from the most recent snapshot.
            var aggregate = AggregateFactory <T> .CreateAggregate();

            var snapshotVersion = RestoreAggregateFromSnapshot(aggregateId, aggregate);

            // If there is no snapshot then load the aggregate directly from the event store.
            if (snapshotVersion == -1)
            {
                return(_eventRepository.Get <T>(aggregateId));
            }

            // Otherwise load the aggregate from the events that occurred after the snapshot was taken.
            var events = (_eventStore.Get(aggregateId, snapshotVersion))
                         .Where(desc => desc.AggregateVersion > snapshotVersion);

            aggregate.Rehydrate(events);

            return(aggregate);
        }
Exemple #5
0
        protected virtual TAggregateRoot LoadAggregate <TAggregateRoot>(Guid id, IList <IEvent <TAuthenticationToken> > events = null)
            where TAggregateRoot : IAggregateRoot <TAuthenticationToken>
        {
            var aggregate = AggregateFactory.CreateAggregate <TAggregateRoot>(id);

            LoadAggregateHistory(aggregate, events);
            return(aggregate);
        }
        /// <summary>
        /// Retrieves an aggregate from offline storage and returns only its most recent state.
        /// </summary>
        public T Unbox <T>(Guid aggregateId) where T : AggregateRoot
        {
            var snapshot  = _snapshotStore.Unbox(aggregateId, typeof(T));
            var aggregate = AggregateFactory <T> .CreateAggregate();

            aggregate.AggregateIdentifier = aggregateId;
            aggregate.AggregateVersion    = 1;
            aggregate.State = snapshot.AggregateState;
            return(aggregate);
        }
Exemple #7
0
        /// <summary>
        /// Retrieves an aggregate from offline storage and returns only its most recent state.
        /// </summary>
        public T Unbox <T>(Guid aggregateId) where T : AggregateRoot
        {
            var snapshot  = _snapshotStore.Unbox(aggregateId);
            var aggregate = AggregateFactory <T> .CreateAggregate();

            aggregate.AggregateIdentifier = aggregateId;
            aggregate.AggregateVersion    = 1;
            aggregate.State = _eventStore.Serializer.Deserialize <AggregateState>(snapshot.AggregateState, aggregate.CreateState().GetType());
            return(aggregate);
        }
Exemple #8
0
        private async Task <T> LoadAggregate <T>(Guid id, CancellationToken cancellationToken = default(CancellationToken)) where T : AggregateRoot
        {
            var events = await _eventStore.Get(id, -1, cancellationToken);

            if (!events.Any())
            {
                throw new AggregateNotFoundException(typeof(T), id);
            }
            var aggregate = AggregateFactory.CreateAggregate <T>();

            aggregate.LoadFromHistory(events);
            return(aggregate);
        }
Exemple #9
0
        protected Task <AggregateRoot> BuildFromHistory(Type aggregateType, string aggregateId, IEnumerable <IEvent> history)
        {
            if (history == null || !history.Any())
            {
                throw new AggregateNotFoundException(aggregateId, typeof(TAggregate));
            }

            var result = AggregateFactory.CreateAggregate(aggregateType);

            result.ApplyChanges(history);

            return(Task.FromResult(result));
        }
Exemple #10
0
        private T LoadAggregate <T>(Guid id) where T : AggregateRoot
        {
            var aggregate = AggregateFactory.CreateAggregate <T>();

            var events = _eventStore.Get <T>(id, -1);

            if (!events.Any())
            {
                throw new AggregateNotFoundException(id);
            }

            aggregate.LoadFromHistory(events);
            return(aggregate);
        }
        protected async Task <TAggregate> GetById(string streamName, int version)
        {
            if (version <= 0)
            {
                throw new InvalidOperationException("Cannot get version <= 0");
            }

            long sliceStart = StreamPosition.Start;
            StreamEventsSlice currentSlice;
            AggregateRoot     aggregate = null;

            do
            {
                var sliceCount = sliceStart + READ_PAGE_SIZE <= version
                                    ? READ_PAGE_SIZE
                                    : (int)(version - sliceStart + 1);

                currentSlice = await EventStoreConnection.ReadStreamEventsForwardAsync(streamName, sliceStart, sliceCount, false);

                if (currentSlice.Status == SliceReadStatus.StreamNotFound)
                {
                    throw new AggregateNotFoundException(streamName, typeof(TAggregate));
                }

                if (currentSlice.Status == SliceReadStatus.StreamDeleted)
                {
                    throw new AggregateDeletedException(streamName, typeof(TAggregate));
                }

                sliceStart = currentSlice.NextEventNumber;

                var domainEvents = currentSlice.Events.Select(e => EventSerializer.DeserializeEvent(e.OriginalEvent)).ToList();
                if (aggregate == null)
                {
                    aggregate = AggregateFactory.CreateAggregate <TAggregate>();
                }
                else
                {
                    await ApplyChangesToAggregate(aggregate, domainEvents);
                }
            } while (version >= currentSlice.NextEventNumber && !currentSlice.IsEndOfStream);

            if (version < Int32.MaxValue && aggregate.Version != version)
            {
                throw new AggregateVersionException(streamName, typeof(TAggregate), aggregate.Version, version);
            }

            return((TAggregate)aggregate);
        }
        public T Get <T>(Guid aggregateId) where T : AggregateRoot
        {
            var aggregate       = AggregateFactory.CreateAggregate <T>();
            var snapshotVersion = TryRestoreAggregateFromSnapshot(aggregateId, aggregate);

            if (snapshotVersion == -1)
            {
                return(_domainRepository.Get <T>(aggregateId));
            }
            var events = _domainEventStore.Get(aggregateId, snapshotVersion).Where(desc => desc.Version > snapshotVersion);

            aggregate.LoadFromHistory(events);

            return(aggregate);
        }
        public async Task <T> Get <T>(Guid aggregateId) where T : AggregateRoot
        {
            var aggregate       = AggregateFactory.CreateAggregate <T>();
            var snapshotVersion = await TryRestoreAggregateFromSnapshot(aggregateId, aggregate);

            if (snapshotVersion == -1)
            {
                return(await _repository.Get <T>(aggregateId));
            }
            var events = (await _eventStore.Get(aggregateId, snapshotVersion)).Where(desc => desc.Version > snapshotVersion);

            aggregate.LoadFromHistory(events);

            return(aggregate);
        }
Exemple #14
0
        private async Task <T> LoadAggregate <T>(Guid id) where T : AggregateRoot
        {
            var events = await _eventStore.Get(id, -1);

            var eventList = events as IList <IEvent> ?? events.ToList();

            if (!eventList.Any())
            {
                throw new AggregateNotFoundException(typeof(T), id);
            }

            var aggregate = AggregateFactory.CreateAggregate <T>();

            aggregate.LoadFromHistory(eventList);
            return(aggregate);
        }
        public async Task <T> GetAsync <T>(string aggregateId) where T : AggregateRoot
        {
            var aggregate       = AggregateFactory.CreateAggregate <T>();
            var snapshotVersion = await TryRestoreAggregateFromSnapshot(aggregateId, aggregate).ConfigureAwait(false);

            if (snapshotVersion == -1)
            {
                return(await _repository.GetAsync <T>(aggregateId).ConfigureAwait(false));
            }

            var aggregateType = typeof(T).Name;
            var events        = (await _eventStore.GetAsync(aggregateId, aggregateType, snapshotVersion).ConfigureAwait(false)).Where(desc => desc.Version > snapshotVersion);

            aggregate.LoadFromHistory(events);

            return(aggregate);
        }
Exemple #16
0
        public async Task <T> Get <T>(Guid aggregateId, CancellationToken cancellationToken = default(CancellationToken)) where T : AggregateRoot
        {
            var aggregate = AggregateFactory <T> .CreateAggregate();

            var snapshotVersion = await TryRestoreAggregateFromSnapshot(aggregateId, aggregate, cancellationToken).ConfigureAwait(false);

            if (snapshotVersion == -1)
            {
                return(await _repository.Get <T>(aggregateId, cancellationToken).ConfigureAwait(false));
            }

            var events = (await _eventStore.Get(aggregateId, snapshotVersion, cancellationToken).ConfigureAwait(false))
                         .Where(desc => desc.Version > snapshotVersion);

            aggregate.LoadFromHistory(events);

            return(aggregate);
        }
Exemple #17
0
        protected override TAggregateRoot LoadAggregate <TAggregateRoot>(Guid id, IList <IEvent <TAuthenticationToken> > events = null)
        {
            var aggregate = AggregateFactory.CreateAggregate <TAggregateRoot>();

            /*
             * IList<IEvent<TAuthenticationToken>> theseEvents = events ?? EventStore.Get<TAggregateRoot>(id).ToList();
             * if (!theseEvents.Any())
             *      throw new AggregateNotFoundException<TAggregateRoot, TAuthenticationToken>(id);
             *
             * var duplicatedEvents =
             *      theseEvents.GroupBy(x => x.Version)
             *              .Select(x => new { Version = x.Key, Total = x.Count() })
             *              .FirstOrDefault(x => x.Total > 1);
             * if (duplicatedEvents != null)
             *      throw new DuplicateEventException<TAggregateRoot, TAuthenticationToken>(id, duplicatedEvents.Version);
             *
             * aggregate.LoadFromHistory(theseEvents);
             */
            return(aggregate);
        }
Exemple #18
0
        protected override TAggregateRoot CreateAggregate <TAggregateRoot>(Guid id)
        {
            var aggregate = AggregateFactory.CreateAggregate <TAggregateRoot>();

            return(aggregate);
        }