private void SaveEvents(Guid aggregateId, long uncomittedVersion, IEnumerable <IEvent> uncommittedEvents) { using (var ctx = new EventStoreContext()) { long eventVersion = uncomittedVersion; foreach (IEvent evnt in uncommittedEvents) { eventVersion++; evnt.Id = aggregateId; evnt.Version = eventVersion; ctx.AddToEvents(new Event { AggregateId = evnt.Id, Version = evnt.Version, Data = ToBinary(converter.Convert(evnt)), Type = evnt.GetType().Name }); } ctx.SaveChanges(); } }
/// <summary> /// Gets the current version of an aggregate root from the store. /// </summary> public long GetVersion(Guid id) { using (var ctx = new EventStoreContext()) { var q = from evt in ctx.Events where (evt.AggregateId == id) select evt.Version; return((q.Count() > 0) ? q.Max() : 0); } }
/// <summary> /// Get all events provided by an specified event source for a particular version, since an earlier version. /// </summary> /// <param name="eventSourceId">The id of the event source that owns the events.</param> /// <param name="fromVersion"></param> /// <param name="version"></param> /// <returns>All the events from the event source.</returns> public IEnumerable <IEvent> GetAllEventsForVersionSince(Guid eventSourceId, long fromVersion, long version) { using (var ctx = new EventStoreContext()) { var q = from evt in ctx.Events where (evt.AggregateId == eventSourceId) && (evt.Version > fromVersion) && (evt.Version <= version) orderby evt.Version ascending select evt.Data; return(q.ToList().Select(data => converter.Convert(FromBinary(data))).Cast <IEvent>().ToArray()); } }
private Snapshot FindSnapshotForVersion(Guid eventSourceId, long version) { using (var ctx = new EventStoreContext()) { var snapshots = from snapshot in ctx.Snapshots where (snapshot.AggregateId == eventSourceId) && (snapshot.Version <= version) orderby snapshot.Version descending select snapshot; return(snapshots.FirstOrDefault()); } }
/// <summary> /// Saves a snapshot of the specified event source. /// </summary> public void SaveShapShot(IEventSource source) { using (var ctx = new EventStoreContext()) { IMemento memento = source.GetMemento(); ctx.AddToSnapshots(new Snapshot { Data = ToBinary(converter.Convert(memento)), AggregateId = source.Id, Version = source.Version, Type = memento.GetType().Name }); ctx.SaveChanges(); } }