private void SaveEvent(CommandBusinessContext[] contexts) { var evs = contexts.SelectMany(a => a.UncommittedEvents ?? Enumerable.Empty <IEvent>()).ToList(); if (evs == null || !evs.Any()) { return; } try { _eventStore.AddRange(evs); } catch (Exception ex) { foreach (var item in contexts) { item.Reply.Exception = ex; item.Reply.IsSuccess = false; } _logger.LogError(ex, $"存储事件失败,错误消息:{ex.Message}"); } }
public async Task <IReadOnlyList <Result <OptimisticConcurrencyErrorDesc> > > AddRange(IReadOnlyList <IEvent> events) { await _evStore.AddRange(events); var grouped = events.Cast <EndorsementAddedOrRemovedEvent>() .GroupBy(x => x.TargetId) .ToList(); var results = grouped.Select ( async iG => { var snapshot = await _CreateSnapshot(iG.Key, iG.ToList()); var leftSnapshot = _snaphotsByIdMap.AddOrUpdate(iG.Key, snapshot, (xId, xExisting) => _ChooseSnapshot(snapshot, xExisting)); if (leftSnapshot != snapshot) { return(Result <OptimisticConcurrencyErrorDesc> .Error(new OptimisticConcurrencyErrorDesc())); } else { return(Result <OptimisticConcurrencyErrorDesc> .Ok); } } ).ToList(); return(await Task.WhenAll(results)); }