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); }