Пример #1
0
        public async Task <IEventStream> GetStream <T>(String bucket, String stream, Int32?start = null) where T : class, IEntity
        {
            Logger.DebugFormat("Getting stream '{0}' in bucket '{1}'", stream, bucket);

            var streamId = String.Format("{0}.{1}", bucket, stream);
            var events   = new List <ResolvedEvent>();

            var readSize = _nsbSettings.Get <Int32>("ReadSize");

            if (_shouldCache)
            {
                var cached = _cache.Retreive(streamId);
                if (cached != null)
                {
                    return(cached);
                }
            }

            var settings = new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All,
                Binder           = new EventSerializationBinder(_mapper),
                ContractResolver = new EventContractResolver(_mapper)
            };

            StreamEventsSlice current;
            var sliceStart = start ?? StreamPosition.Start;

            do
            {
                current = await _client.ReadStreamEventsForwardAsync(streamId, sliceStart, readSize, false);

                events.AddRange(current.Events);
                sliceStart = current.NextEventNumber;
            } while (!current.IsEndOfStream);

            var translatedEvents = events.Select(e =>
            {
                var descriptor = e.Event.Metadata.Deserialize(settings);
                var data       = e.Event.Data.Deserialize(e.Event.EventType, settings);

                return(new Internal.WritableEvent
                {
                    Descriptor = descriptor,
                    Event = data,
                    EventId = e.Event.EventId
                });
            });

            var eventstream = new Internal.EventStream <T>(_builder, this, _snapshots, bucket, stream, current.LastEventNumber, translatedEvents);

            if (_shouldCache)
            {
                _cache.Cache(streamId, eventstream.Clone());
            }

            return(eventstream);
        }