public void SaveEvents(Guid aggregateId, IEnumerable <IEvent> events, int expectedVersion) { // Load count of events in the eventsore, for the aggregate with the specified id (To compare versions) var currentVersionInStore = ExecutedEvents.Count(x => x.AggregateId == aggregateId); if (currentVersionInStore != expectedVersion) { throw new ConcurrencyException(); } var i = expectedVersion; foreach (var @event in events) { i++; ExecutedEvents.Add(new ExecutedEvent { AggregateId = aggregateId, Body = JsonConvert.SerializeObject(@event), BodyType = @event.GetType().GetTypeInfo().Assembly.FullName + "|" + @event.GetType().FullName, Executed = DateTime.UtcNow, Version = i }); SaveChanges(); _eventPublisher.Publish(@event); } }
public List <IEvent> GetEventsForAggregate(Guid aggregateId, int fromVersion = 0) { Dictionary <string, string> eventListAsStrings = ExecutedEvents.Where(x => x.AggregateId == aggregateId).ToList().ToDictionary(x => x.BodyType, x2 => x2.Body); List <IEvent> result = new List <IEvent>(); foreach (var loopObjectAsString in eventListAsStrings) { Type eventType = Assembly.Load(new AssemblyName(loopObjectAsString.Key.Split('|')[0])).GetType(loopObjectAsString.Key.Split('|')[1]); result.Add((IEvent)JsonConvert.DeserializeObject(loopObjectAsString.Value, eventType)); } if (result.Count == 0) { throw new NoEventsFoundForAggregateException(); } return(result); }