/// <summary> /// Save event source object using <see cref="RaraAvis.nCubed.EventSourcing.Core.Mementos.IMemento"/> if available. /// </summary> /// <param name="eventSourced">A source object.</param> /// <param name="correlationId">Related correlation id.</param> public void Save(T eventSourced, string correlationId) { CoreExceptionProcessor.ProcessInfrastructure(() => { using (var context = new EventSourcingContext()) { var eventsSet = context.Set <EventData>(); while (eventSourced.Events.Count() > 0) { try { var versionedEvent = eventSourced.Events.Dequeue(); versionedEvent.SourceId = versionedEvent.SourceId == Guid.Empty ? eventSourced.Id : versionedEvent.SourceId; this.messagingRandomRetry.ExecuteAction(() => this.eventBus.Publish(new Envelope <IEvent>(versionedEvent) { CorrelationId = correlationId })); eventsSet.Add(this.Serialize(versionedEvent, correlationId)); } catch (Exception ex) { try { this.sqlIncrementalRetry.ExecuteAction(() => { context.SaveChanges(); }); //Save processed store events } catch (Exception exSql) { Exception exSqlOut = null; EventSourcingExceptionProcessor.HandleException(exSql, out exSqlOut); throw exSqlOut; } Exception exOut = null; EventSourcingExceptionProcessor.HandleException(ex, out exOut); throw exOut; } } try { this.sqlIncrementalRetry.ExecuteAction(() => { context.SaveChanges(); }); saveMemento(eventSourced); } catch (Exception ex) { Exception exOut = null; EventSourcingExceptionProcessor.HandleException(ex, out exOut); throw exOut; } } }); }
protected void Commit(Entity entity, string action, Entity lastEntity = null) { if (_notifications.HasNotifications()) { return; } if (_architectureContext.SaveChanges() > 0) { var eventEntity = new EventEntity(action, entity, "Marcos", lastEntity); _eventSourcingContext.EventEntities.Add(eventEntity); _eventSourcingContext.SaveChanges(); } else { AddNotification(new DomainNotification("Commit", "We had a problem during saving your data.")); } }
public void Save(Event @event, object data) { //var ignoredMembersList = GetAttributs(@event).Select(_=>_.Name); var listIgnoredMembers = GetAttributs(@event).Select(_ => _.Name); var all = _eventSourcingContext.StoredEvent.Where(x => x.AggregateId == @event.AggregateId).OrderByDescending(x => x.Data).ToArray(); if (data != null) { var props = data.GetType().GetProperties().Select(x => new KeyValuePair <string, object>(x.Name, x.GetValue(data, new object[] { }))) .Where(x => !listIgnoredMembers.Contains(x.Key)); foreach (var prop in props) { var getPropSave = @event.GetType().GetProperty(prop.Key)?.GetValue(@event, null); if ((prop.Value == null && getPropSave != null) && LastChangeIsEqual(prop.Key, all)) { var propEvent = @event.GetType().GetProperty(prop.Key, BindingFlags.Public | BindingFlags.Instance); propEvent.SetValue(@event, null, null); } if ((prop.Value?.ToString() == getPropSave?.ToString() && (prop.Value != null && getPropSave != null) && prop.Key != "AggregateId" && prop.Key != "Who" && prop.Key != "id")) { var propEvent = @event.GetType().GetProperty(prop.Key, BindingFlags.Public | BindingFlags.Instance); if (null != propEvent && propEvent.CanWrite) { propEvent.SetValue(@event, null, null); } } } } _eventSourcingContext.StoredEvent.Add(new StoredEvent(@event, "name user")); _eventSourcingContext.SaveChanges(); }
public void Save(Event @event) { _eventSourcingContext.StoredEvent.Add(new StoredEvent(@event, _user.Name)); _eventSourcingContext.SaveChanges(); }
public void Store(StoredEvent theEvent) { _context.StoredEvent.Add(theEvent); _context.SaveChanges(); }