Beispiel #1
0
        private bool TryGetFromCompatibleAggregateStore(string aggregateRootId, Type aggregateRootType,
                                                        ICompatibleStoreHandler aggregateInitHandler, CompatibleStyle compatibleStyle, out IAggregateRoot aggregateRoot)
        {
            aggregateRoot = null;
            var aggregateRootTypeCode = _aggregateRootTypeCodeProvider.GetTypeCode(aggregateRootType);

            if (compatibleStyle == CompatibleStyle.RepositoryOnly)
            {
                // Replay Repository Aggregate Init Event
                DomainEventStream aggregateInitEvent = aggregateInitHandler.GetAggregateRestoreEventStream(aggregateRootId);
                if (aggregateInitEvent != null)
                {
                    if (aggregateInitEvent.AggregateRootId != aggregateRootId)
                    {
                        throw new Exception(string.Format("DomainEventStream.AggregateRootId error: {0}, expected aggregate root id:{1}", aggregateInitEvent.AggregateRootId, aggregateRootId));
                    }
                    aggregateRoot = _aggregateRootFactory.CreateAggregateRoot(aggregateRootType);
                    aggregateRoot.RestoreFromEvents(aggregateInitEvent);
                    return(true);
                }
                return(false);
            }
            else if (compatibleStyle == CompatibleStyle.RepositoryThenEventSourcing)
            {
                var versionStart = minVersion;

                // Replay Repository Aggregate Init Event
                //IDomainEvent aggregateInitEvent = _compatibleAggregateStore.GetAggregateInitEvent(aggregateRootType, aggregateRootId);
                DomainEventStream aggregateInitEvent = aggregateInitHandler.GetAggregateRestoreEventStream(aggregateRootId);
                if (aggregateInitEvent != null)
                {
                    if (aggregateInitEvent.AggregateRootId != aggregateRootId)
                    {
                        throw new Exception(string.Format("DomainEventStream.AggregateRootId error: {0}, expected aggregate root id:{1}", aggregateInitEvent.AggregateRootId, aggregateRootId));
                    }
                    aggregateRoot = _aggregateRootFactory.CreateAggregateRoot(aggregateRootType);
                    aggregateRoot.RestoreFromEvents(aggregateInitEvent);
                    versionStart = aggregateInitEvent.Version + 1;
                }

                // Replay Event Sourcing Events
                var eventStreams = _eventStore.QueryAggregateEvents(aggregateRootId, aggregateRootTypeCode, versionStart, maxVersion);
                if (eventStreams == null || !eventStreams.Any())
                {
                    if (aggregateRoot != null)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                aggregateRoot.ReplayEvents(eventStreams);
                return(true);
            }
            else //if(compatibleStyle == CompatibleAggregateInitStyle.EventSourcingOnly)
            {
                // Replay Event Sourcing Events
                var versionStart = minVersion;
                var eventStreams = _eventStore.QueryAggregateEvents(aggregateRootId, aggregateRootTypeCode, versionStart, maxVersion);
                if (eventStreams == null || !eventStreams.Any())
                {
                    return(false);
                }
                aggregateRoot = _aggregateRootFactory.CreateAggregateRoot(aggregateRootType);
                aggregateRoot.ReplayEvents(eventStreams);
                return(true);
            }
        }