public Task <IDomainEvent <IDomainEventData> > Store(ICreateEvent <IDomainEventData> newEvent) { var eventData = newEvent.Data; var eventVersion = _events.Where(x => x.AggregateKey == eventData.GetAggregateKey()).Select(x => (ulong?)x.Version).Max(x => x) ?? 0; Instant created = _clock.GetCurrentInstant(); var @event = _eventFactory.CreateEvent(eventVersion, created, eventData); _events.Add(@event); _sender.SendEvent(@event); var result = _eventFactory.CreateEvent(eventVersion, created, eventData); return(Task.FromResult(result)); }
public async Task <IDomainEvent <IDomainEventData> > Store(ICreateEvent <IDomainEventData> newEvent) { var eventData = newEvent.Data; var dataJson = _eventFactory.SerializeToData(eventData); var data = Encoding.UTF8.GetBytes(dataJson); var metadata = Encoding.UTF8.GetBytes("{}"); var eventId = Guid.NewGuid(); var eventName = _eventFactory.EventName(eventData); var evt = new EventData(Uuid.FromGuid(eventId), eventName, data, metadata); var eventStream = eventData.GetAggregateKey(); IWriteResult result; if (newEvent.IsNewAggregate) { result = await _client.AppendToStreamAsync(eventStream, StreamState.NoStream, new List <EventData>() { evt }); } else if (newEvent.IsVersioned) { result = await _client.AppendToStreamAsync(eventStream, new StreamRevision(newEvent.Version), new List <EventData>() { evt }); } else { result = await _client.AppendToStreamAsync(eventStream, StreamState.StreamExists, new List <EventData>() { evt }); } _logger.LogDebug("Appended event {position}|{type}.", result.LogPosition, evt.Type); return(_eventFactory.CreateEvent(result.NextExpectedStreamRevision.ToUInt64(), SystemClock.Instance.GetCurrentInstant(), eventData)); }