using Flensrocker.EventSourcing.Abstractions; using Flensrocker.EventSourcing.Orleans.Abstractions; using Microsoft.Extensions.DependencyInjection; using Orleans.EventSourcing; using Orleans.EventSourcing.CustomStorage; namespace Flensrocker.EventSourcing.Orleans.Server; public abstract class CommandHandlerGrain : JournaledGrain, ICustomStorageInterface, ICommandHandlerGrain where TEventBase : class where TCommandBase : class where TCommandState : class, IDomainEventProjectable, new() { private readonly IDomainEventStore _eventStore; private int _nextVersion; protected CommandHandlerGrain() { _eventStore = ServiceProvider.GetRequiredService>(); } public override async Task OnActivateAsync(CancellationToken cancellationToken) { await base.OnActivateAsync(cancellationToken); _nextVersion = Version + 1; } protected void RaiseDomainEvents(DateTimeOffset createdAt, TEventBase[] payloads) { var tenantId = OrleansRequestContextExtensions.GetActiveTenant(); var userId = OrleansRequestContextExtensions.GetUser()?.UserId; // TODO Enable adding public messages to domain events. var streamId = this.GetPrimaryKey(); if (payloads.Length == 1) { var domainEvent = DomainEvent.Create(_eventStore.StreamType, streamId, _nextVersion, payloads[0], tenantId, userId, createdAt); _nextVersion++; RaiseEvent(domainEvent); } else { var domainEventList = new List(payloads.Length); foreach (var payload in payloads) { var domainEvent = DomainEvent.Create(_eventStore.StreamType, streamId, _nextVersion, payload, tenantId, userId, createdAt); _nextVersion++; domainEventList.Add(domainEvent); } RaiseEvents(domainEventList); } } public async Task> ExecuteCommand(TCommandBase command) { await using var commandScope = ServiceProvider.CreateAsyncScope(); var commandHandler = commandScope.ServiceProvider.GetRequiredService>(); return await commandHandler .ExecuteCommandAsync(State, command) .Pipe(async payloads => { if (payloads.Length > 0) { var timeProvider = commandScope.ServiceProvider.GetRequiredService(); var createdAt = timeProvider.GetUtcNow(); RaiseDomainEvents(createdAt, payloads); await ConfirmEvents(); } return new CommandResult { Version = _nextVersion - 1 }; }); } Task ICustomStorageInterface.ApplyUpdatesToStorage( IReadOnlyList updates, int expectedVersion ) => _eventStore.ApplyUpdatesToStorage(this.GetPrimaryKey(), updates, expectedVersion); Task> ICustomStorageInterface.ReadStateFromStorage() => _eventStore.ReadStateFromStorage(this.GetPrimaryKey(), default); }