public virtual async Task <TAggregateRoot> GetByIdAsync(Guid id) { //获取快照 var memento = await mementoStorage.GetMementoAsync(id); //获取事件 var events = await eventStorage.GetEventsAsync(id); //还原事件 var result = Restore(events, memento); return(result); }
public virtual async Task <TAggregate> GetAsync(Guid aggregateId) { TAggregate aggregate = null; var startEvent = 0; if (aggregateFactory.IsSnapshottable()) { var snapshot = await snapshotStorage.GetSnapshotAsync(aggregateFactory.GetSnapshotType(), aggregateId); if (snapshot != null) { aggregate = aggregateFactory.FromSnapshot((Snapshot <TAggregate>)snapshot); startEvent = snapshot.Version; } } var events = (await eventStorage.GetEventsAsync(aggregateId, startEvent, int.MaxValue)).ToList(); if (aggregate == null && !events.Any()) { throw new AggregateNotFoundException($"Aggregate with id '{aggregateId}' not found in repository"); } aggregate = aggregate ?? aggregateFactory.Create(aggregateId); aggregate.Hydrate(events); AddToTracking(aggregate); return(aggregate); }
public async Task <T> GetByKeyAsync(Guid aggregateKey) { T aggregate; if (!_memoryCache.TryGetValue <T>(aggregateKey, out aggregate)) { Memento memento = await _storage.GetMementoAsync(aggregateKey); using (var ms = new MemoryStream(memento.AggregateBinary)) { IFormatter iFormatter = new BinaryFormatter(); aggregate = iFormatter.Deserialize(ms) as T; } IEnumerable <Event> events = await _storage.GetEventsAsync(aggregateKey, memento.Version); foreach (Event e in events.OrderBy(o => o.Version)) { aggregate.ReplayEvent(e); } } return(aggregate); }
public Task <IQueryable <Event> > GetEventsAsync(Guid aggregateId, int start, int count) => storage.GetEventsAsync(aggregateId, start, count);