Esempio n. 1
0
        public override IEnumerable <IEvent <TAuthenticationToken> > Get(Type aggregateRootType, Guid aggregateId, bool useLastEventOnly = false, int fromVersion = -1)
        {
            string streamName = string.Format(CqrsEventStoreStreamNamePattern, aggregateRootType.FullName, aggregateId);

            if (!EventStoreByType.Contains(streamName))
            {
                Logger.LogDebug(string.Format("The event store has no items '{0}'.", streamName));
                return(Enumerable.Empty <IEvent <TAuthenticationToken> >());
            }

            CacheItem item = EventStoreByType.GetCacheItem(streamName);

            if (item == null)
            {
                Logger.LogDebug(string.Format("The event store had an item '{0}' but doesn't now.", streamName));
                return(Enumerable.Empty <IEvent <TAuthenticationToken> >());
            }

            var events = item.Value as IEnumerable <EventData>;

            if (events == null)
            {
                if (item.Value == null)
                {
                    Logger.LogDebug(string.Format("The event store had an item '{0}' but it was null.", streamName));
                }
                else
                {
                    Logger.LogWarning(string.Format("The event store had an item '{0}' but it was of type {1}.", streamName, item.Value.GetType()));
                }
                return(Enumerable.Empty <IEvent <TAuthenticationToken> >());
            }
            IEnumerable <EventData> query = events
                                            .Where(eventData => eventData.Version > fromVersion)
                                            .OrderByDescending(eventData => eventData.Version);

            if (useLastEventOnly)
            {
                query = query.AsQueryable().Take(1);
            }

            return(query
                   .Select(EventDeserialiser.Deserialise)
                   .ToList());
        }
Esempio n. 2
0
        protected override void PersistEvent(EventData eventData)
        {
            IList <EventData> events = new List <EventData>();

            // By correlationId first
            Guid   correlationId = eventData.CorrelationId;
            object item          = EventStoreByCorrelationId.AddOrGetExisting(correlationId.ToString("N"), events, GetDetaultCacheItemPolicy());

            if (item != null)
            {
                events = item as IList <EventData>;
                if (events == null)
                {
                    Logger.LogWarning(string.Format("The event store had some items by the correlationId '{0:N}' but it doesn't now.", correlationId));
                    throw new Exception(string.Format("The event store had some items by the correlationId '{0:N}' but it doesn't now.", correlationId));
                }
            }

            events.Add(eventData);
            // Reset the variable for it's next usage
            events = new List <EventData>();

            // By type next
            string streamName = eventData.AggregateId;

            item = EventStoreByType.AddOrGetExisting(streamName, events, GetDetaultCacheItemPolicy());
            if (item != null)
            {
                events = item as IList <EventData>;
                if (events == null)
                {
                    Logger.LogWarning(string.Format("The event store had an item by id '{0}' but it doesn't now.", streamName));
                    throw new Exception(string.Format("The event store had an item by id '{0}' but it doesn't now.", streamName));
                }
            }

            events.Add(eventData);
        }