public async Task WriteEvents(String bucket, String stream, Int32 expectedVersion, IEnumerable <IWritableEvent> events, IDictionary <String, String> commitHeaders) { Logger.DebugFormat("Writing {0} events to stream id '{1}'. Expected version: {2}", events.Count(), stream, expectedVersion); var streamId = String.Format("{0}.{1}", bucket, stream); if (_shouldCache) { _cache.Evict(streamId); } var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, Binder = new EventSerializationBinder(_mapper) }; var translatedEvents = events.Select(e => { var descriptor = new EventDescriptor { EntityType = e.Descriptor.EntityType, Timestamp = e.Descriptor.Timestamp, Version = e.Descriptor.Version, Headers = e.Descriptor.Headers.Merge(commitHeaders) }; var mappedType = _mapper.GetMappedTypeFor(e.Event.GetType()); return(new EventData( e.EventId, mappedType.AssemblyQualifiedName, true, e.Event.Serialize(settings).AsByteArray(), descriptor.Serialize(settings).AsByteArray() )); }); try { await _client.AppendToStreamAsync(streamId, expectedVersion, translatedEvents); } catch (global::System.AggregateException e) { throw e.InnerException; } }
public async Task WriteSnapshots(String bucket, String stream, IEnumerable <ISnapshot> snapshots, IDictionary <String, String> commitHeaders) { Logger.DebugFormat("Writing {0} snapshots to stream id '{1}' in bucket '{2}'", snapshots.Count(), stream, bucket); var streamId = String.Format("{0}.{1}.{2}", bucket, stream, "snapshots"); if (_shouldCache) { _cache.EvictSnap(streamId); } var translatedEvents = snapshots.Select(e => { var descriptor = new EventDescriptor { EntityType = e.EntityType, Timestamp = e.Timestamp, Version = e.Version, Headers = commitHeaders }; return(new EventData( Guid.NewGuid(), e.EntityType, true, e.Payload.Serialize(_settings).AsByteArray(), descriptor.Serialize(_settings).AsByteArray() )); }); try { await _client.AppendToStreamAsync(streamId, ExpectedVersion.Any, translatedEvents); } catch (global::System.AggregateException e) { throw e.InnerException; } }