public async Task UpdateAsync() { var domainEventType = $"QuerryHandler-{typeof(TQuerry).Name}-{typeof(TEvent).Name}"; var lastVersion = await _versionRepository.GetVersionAsync(domainEventType); var latestEvents = await _eventFeed.GetEventsAsync(lastVersion); var domainEvents = latestEvents.ToList(); if (!domainEvents.Any()) { return; } var querry = await _readModelRepository.LoadAsync <TQuerry>(); if (querry.Is <NotFound>()) { querry = Result <TQuerry> .Ok(new TQuerry()); } var querryValue = querry.Value; foreach (var latestEvent in domainEvents) { querryValue.Handle(latestEvent.DomainEvent, latestEvent.EntityStreamVersion); await _versionRepository.SaveVersion(new LastProcessedVersion(domainEventType, latestEvent.OverallVersion)); } await _readModelRepository.SaveQueryAsync(querryValue); }
public async Task UpdateAsync() { var redaModelVersionCounter = $"ReadModelVersion-{typeof(T).Name}"; var lastVersion = await _versionRepository.GetVersionAsync(redaModelVersionCounter); var latestEvents = await _eventFeed.GetEventsAsync(lastVersion); var domainEvents = latestEvents.ToList(); if (!domainEvents.Any()) { return; } foreach (var latestEvent in domainEvents) { var latestEventDomainEvent = latestEvent.DomainEvent; var domainEventEntityId = latestEventDomainEvent.EntityId; var latestEventVersion = latestEvent.EntityStreamVersion; var result = new T().GetsCreatedOn == latestEventDomainEvent.GetType() ? Result <T> .Ok(new T()) : await _readModelRepository.LoadAsync <T>(domainEventEntityId); if (result.Is <NotFound>()) { continue; } var readModel = result.Value; readModel.Handle(latestEventDomainEvent, latestEventVersion); if (latestEventVersion < result.Value.Version) { latestEventVersion = result.Value.Version; } readModel.Version = latestEventVersion; readModel.Identity = domainEventEntityId; await _readModelRepository.SaveReadModelAsync(readModel); await _versionRepository.SaveVersion(new LastProcessedVersion(redaModelVersionCounter, latestEvent.OverallVersion)); } }
public async Task UpdateAsync() { var handleType = _handler.GetType(); var domainEventType = $"{handleType.Name}-{typeof(TSubscribedDomainEvent).Name}"; var lastVersion = await _versionRepository.GetVersionAsync(domainEventType); var latestEvents = await _eventFeed.GetEventsAsync(lastVersion); foreach (var latestEvent in latestEvents) { if (latestEvent.DomainEvent is TSubscribedDomainEvent domainEvent) { await _handler.HandleAsync(domainEvent); await _versionRepository.SaveVersion(new LastProcessedVersion(domainEventType, latestEvent.OverallVersion)); } } }