public void SavesCommitHeadersOnEachEvent() { var commitId = Guid.NewGuid(); var aggregateToSave = new TestAggregate(Guid.NewGuid()); aggregateToSave.ProduceEvents(20); _repo.Save(aggregateToSave, commitId, d => { d.Add("CustomHeader1", "CustomValue1"); d.Add("CustomHeader2", "CustomValue2"); }); var read = _connection.ReadStreamEventsForward(string.Format("aggregate-{0}", aggregateToSave.Id), 1, 20, false); foreach (var serializedEvent in read.Events) { var parsedMetadata = JObject.Parse(Encoding.UTF8.GetString(serializedEvent.OriginalEvent.Metadata)); var deserializedCommitId = parsedMetadata.Property("CommitId").Value.ToObject <Guid>(); Assert.AreEqual(commitId, deserializedCommitId); var deserializedCustomHeader1 = parsedMetadata.Property("CustomHeader1").Value.ToObject <string>(); Assert.AreEqual("CustomValue1", deserializedCustomHeader1); var deserializedCustomHeader2 = parsedMetadata.Property("CustomHeader2").Value.ToObject <string>(); Assert.AreEqual("CustomValue2", deserializedCustomHeader2); } }
public TAggregate GetById <TAggregate>(Guid id, int version) where TAggregate : class, IAggregate { if (version <= 0) { throw new InvalidOperationException("Cannot get version <= 0"); } var streamName = _aggregateIdToStreamName(typeof(TAggregate), id); var aggregate = ConstructAggregate <TAggregate>(); var sliceStart = 1; //Ignores $StreamCreated StreamEventsSlice currentSlice; do { var sliceCount = sliceStart + ReadPageSize <= version ? ReadPageSize : version - sliceStart + 1; currentSlice = _eventStoreConnection.ReadStreamEventsForward(streamName, sliceStart, sliceCount, false); if (currentSlice.Status == SliceReadStatus.StreamNotFound) { throw new AggregateNotFoundException(id, typeof(TAggregate)); } if (currentSlice.Status == SliceReadStatus.StreamDeleted) { throw new AggregateDeletedException(id, typeof(TAggregate)); } sliceStart = currentSlice.NextEventNumber; foreach (var evnt in currentSlice.Events) { aggregate.ApplyEvent(DeserializeEvent(evnt.OriginalEvent.Metadata, evnt.OriginalEvent.Data)); } } while (version >= currentSlice.NextEventNumber && !currentSlice.IsEndOfStream); if (aggregate.Version != version && version < Int32.MaxValue) { throw new AggregateVersionException(id, typeof(TAggregate), aggregate.Version, version); } return(aggregate); }
public IEnumerable <Event> LoadEventsFor <TAggregate>(Guid id) { var streamName = GetStreamName <TAggregate>(id); var results = new List <Event>(); var moreResults = true; while (moreResults) { var eventsForAggregate = esConnection.ReadStreamEventsForward(streamName, 0, 1000, false); if (eventsForAggregate.Status != SliceReadStatus.Success) { return(new List <Event>()); } var events = eventsForAggregate.Events .Where(_ => _.Event.EventType != "$stream-created-implicit") .Select(_ => { var value = BytesToString.ToString(_.Event.Data); var eventType = JsonConvert.DeserializeObject <Type>(BytesToString.ToString(_.Event.Metadata)); // var methodInfo = typeof (JsonConvert).GetMethods() // .Where(info => info.Name == "DeserializeObject") // .Where(info => info.IsGenericMethod) // .Where(info => info.IsStatic) // .Where(info => info.GetParameters().Length == 1 && info.GetParameters().Single().ParameterType == typeof(String)) // .Single(); // // return (Event) methodInfo // .MakeGenericMethod(Type.GetType(type)) // .Invoke(null, new[] {value}); return((Event)JsonConvert.DeserializeObject(value, eventType)); }); results.AddRange(events.ToList()); moreResults = !eventsForAggregate.IsEndOfStream; } return(results); }