public async Task <T> GetByIdAsync <T>(Guid id) where T : Aggregate, new() { var isSnapshottable = typeof(ISnapshottable <T>).GetTypeInfo().IsAssignableFrom(typeof(T).GetTypeInfo()); var aggregate = new T(); Snapshot snapshot = null; if ((isSnapshottable) && (snapshotStore != null)) { snapshot = ((ISnapshottable <T>)aggregate).TakeSnapshot(); snapshot = await snapshotStore.GetSnapshotAsync <T>(snapshot.GetType(), id); } //snapshot exists? if (snapshot != null) { Console.WriteLine(snapshot.Id); var item = (T)Activator.CreateInstance(typeof(T)); ((ISnapshottable <T>)item).ApplySnapshot(snapshot); Console.WriteLine("getting data from snapshot:" + item.Version); var events = await aggregateStore.GetEvents <T>(id.ToString(), snapshot.Version + 1, int.MaxValue); Console.WriteLine("load event over snapshot event count:" + events.Length); item.Load(events); return(item); } return(await aggregateStore.Load <T>(id.ToString())); }
/// <summary> /// Recovers the actor to the latest state /// </summary> /// <returns></returns> public async Task RecoverStateAsync() { var(snapshot, lastSnapshotIndex) = await _snapshotStore.GetSnapshotAsync(_actorId); if (snapshot is not null && _applySnapshot is not null) { Index = lastSnapshotIndex; _applySnapshot(new RecoverSnapshot(snapshot, lastSnapshotIndex)); } var fromEventIndex = Index + 1; await _eventStore.GetEventsAsync( _actorId, fromEventIndex, long.MaxValue, @event => { Index++; _applyEvent?.Invoke(new RecoverEvent(@event, Index)); } ); }
public virtual async Task <T> GetByIdAsync <T>(int id) where T : AggregateRoot { T item = default(T); var isSnapshottable = typeof(ISnapshottable).GetTypeInfo().IsAssignableFrom(typeof(T)); Snapshot.Snapshot snapshot = null; if ((isSnapshottable) && (_snapshotStore != null)) { snapshot = await _snapshotStore.GetSnapshotAsync(typeof(T), id); } if (snapshot != null) { item = ReflectionHelper.CreateInstance <T>(); ((ISnapshottable)item).ApplySnapshot(snapshot); //var events = await _eventStore.GetEventsAsync(typeof(T), id, snapshot.Version + 1, int.MaxValue); var events = await _eventStore.GetAsync <T>(id, snapshot.Version + 1); item.LoadsFromHistory(events); } else { //var events = (await _eventStore.GetEventsAsync(typeof(T), id, 0, int.MaxValue)).ToList(); var events = await _eventStore.GetAsync <T>(id, 0); if (events.Any()) { item = ReflectionHelper.CreateInstance <T>(); item.LoadsFromHistory(events); } } return(item); }