public virtual async Task Save <T>(T aggregate, CancellationToken token = default) where T : class, IEventSourcingAggregateRoot <TId>, new() { _eventStore.AppendEvents(aggregate.Id, aggregate.GetUncommittedEvents()); await _eventStore.CommitEvents(token); await PublishEvents(aggregate, token); }
public async Task <CommitResponse> Commit(CommitEventsRequest request) { var result = await _eventStore.CommitEvents(request, HttpContext.RequestAborted).ConfigureAwait(false); if (result.Failure is not null) { Response.StatusCode = StatusCodes.Status500InternalServerError; } return(result); }
public void SaveAggregate <TAggregate>(TAggregate aggregate) where TAggregate : IAggregate { var eventsToSave = aggregate.UncommittedEvents.ToList(); var expectedVersion = CalculateExpectedVersion(aggregate, eventsToSave); if (expectedVersion >= 0) { var existingEvents = GetEventsForAggregate <TAggregate>(aggregate.AggregateId); var currentversion = existingEvents.Count - 1; if (currentversion != expectedVersion) { throw new WrongExpectedVersionException($"{aggregate.GetType()}:{aggregate.AggregateId}: Expected version {expectedVersion} but the version is {currentversion}"); } } _eventStore.CommitEvents <TAggregate>(eventsToSave); aggregate.ClearUncommittedEvents(); }
public static async Task <CommitEventsResponse> Commit(this IEventStore eventStore, UncommittedEvents events, Dolittle.Runtime.Execution.ExecutionContext executionContext) { var request = new CommitEventsRequest { CallContext = new CallRequestContext { ExecutionContext = executionContext.ToProtobuf() } }; request.Events.AddRange(events.Select(_ => new UncommittedEventContract { Content = _.Content, Public = _.Public, EventType = _.Type.ToProtobuf(), EventSourceId = _.EventSource })); var response = await eventStore.CommitEvents(request, CancellationToken.None).ConfigureAwait(false); return(response); }
public void DequeueAndDispatchCommands() { var commands = _commandQueue.DeQueueCommands(_queueName); foreach (var cmd in commands) { try { _commandDispatcher.ExecuteCommand <TAggregate>(cmd); } catch (Exception ex) { var failureEvent = new EventFailedBase(cmd.AggregateId, ex.Message); failureEvent.Metadata.CausationId = cmd.Metadata.CausationId; failureEvent.Metadata.CorrelationId = cmd.Metadata.CorrelationId; _eventStore.CommitEvents <TAggregate>(new List <IEvent> { failureEvent }); } } }
public void SaveProcessManager <TProcessManager>(TProcessManager processManager) where TProcessManager : IProcessManager { var eventsToSave = processManager.UncommittedEvents.ToList(); var commandsToDispatch = processManager.UndispatchedCommands; var expectedVersion = CalculateExpectedVersion(processManager, eventsToSave); if (expectedVersion >= 0) { var existingEvents = GetEventsForProcessManager <TProcessManager>(processManager.ProcessId); var currentversion = existingEvents.Count; if (currentversion != expectedVersion) { throw new WrongExpectedVersionException($"{processManager.GetType()}:{processManager.ProcessId}: Expected version {expectedVersion} but the version is {currentversion}"); } } _eventStore.CommitEvents <TProcessManager>(eventsToSave); processManager.ClearUncommittedEvents(); DispatchCommands(commandsToDispatch); processManager.ClearUndispatchedCommands(); }
/// <inheritdoc/> public override Task <CommitEventsResponse> Commit(CommitEventsRequest request, ServerCallContext context) => _eventStore.CommitEvents(request, context.CancellationToken);