Something went wrong. Try again.
Event Sourcing Framework on top of Microsoft Orleans and PostgreSQL
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234using System.Reflection;using System.Text.Json;using Flensrocker.EventSourcing.Abstractions;using Flensrocker.EventSourcing.Persistence.EntityFrameworkCore;using Microsoft.EntityFrameworkCore;using Microsoft.EntityFrameworkCore.Migrations;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.DependencyInjection.Extensions;using Microsoft.Extensions.Logging;using Npgsql.Replication.PgOutput.Messages;
namespace Flensrocker.EventSourcing.Persistence.PostgreSQL;
public static class PostgreSQLPersistenceExtensions{ extension(IServiceCollection services) { public EntityFrameworkCoreDomainEventStoreBuilder AddPostgreSQLDomainEventStore( Func<EntityFrameworkCoreDomainEventStoreOptions> configureOptions ) { var builder = services.AddDomainEventStore<PostgreSQLDomainEventStoreDbProvider>( configureOptions: configureOptions, configureDbOptions: () => new EventSourcingDbContextOptions { CreateModel = modelBuilder => ApplyPostgreSQLEventSourcingConfiguration(modelBuilder, false), }, configureDbContext: (connectionString, dbOptions) => { dbOptions.UseNpgsql(connectionString, npgsqlOptions => { npgsqlOptions.MigrationsAssembly(Assembly.GetExecutingAssembly()); npgsqlOptions.MigrationsHistoryTable(HistoryRepository.DefaultTableName, EntityFrameworkCoreConstants.SchemaName); npgsqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorCodesToAdd: null); }); } );
services.AddDbMigration<PostgreSQLPersistenceDbMigration>();
return builder; }
public IServiceCollection AddPostgreSQLDomainMessageSubscriber(Func<PostgreSQLMessageSubscriberOptions> configureOptions) { services.AddSingleton<PostgreSQLMessageSubscriberMetrics>();
var storageOptions = configureOptions(); services.TryAddSingleton(storageOptions);
services.AddHostedService<PostgreSQLMessageSubscriber>();
return services; } }
extension(MigrationBuilder migrationBuilder) { internal void CreatePublication() { migrationBuilder.Sql( $"CREATE PUBLICATION {PostgreSQLPersistenceConstants.PublicationName} FOR TABLE \"{EntityFrameworkCoreConstants.SchemaName}\".\"{EntityFrameworkCoreConstants.MessageTableName}\" WITH (publish = 'insert');" ); }
internal void DropPublication() { migrationBuilder.Sql($"DROP PUBLICATION {PostgreSQLPersistenceConstants.PublicationName};"); } }
extension(ModelBuilder modelBuilder) { public ModelBuilder ApplyPostgreSQLEventSourcingConfiguration(bool excludeFromMigrations) { modelBuilder.ApplyConfiguration(new MessageEntiyConfiguration(modelBuilder, excludeFromMigrations)); return modelBuilder; } }
internal static async Task<(DomainEventMetadata Metadata, object Payload)?> ToDomainEventAsync( this InsertMessage insertMessage, EventSourcingJsonSerializerOptions jsonOptions, ILogger? logger, CancellationToken cancellationToken ) { Guid? eventId = default; string? streamType = default; Guid? streamId = default; int? streamVersion = default; Guid? tenantId = default; DateTimeOffset? createdAt = default; Guid? createdByUserId = default; byte[]? traceId = default; byte[]? spanId = default; string? payloadJson = default;
await foreach (var column in insertMessage.NewRow.ConfigureAwait(false)) { switch (column.GetFieldName()) { case nameof(MessageEntity.Id): { eventId = await column.Get<Guid>(cancellationToken).ConfigureAwait(false); break; } case nameof(MessageEntity.GlobalIndex): { break; } case nameof(MessageEntity.StreamType): { streamType = await column.GetTextReader().ReadToEndAsync(cancellationToken).ConfigureAwait(false); break; } case nameof(MessageEntity.StreamId): { streamId = await column.Get<Guid>(cancellationToken).ConfigureAwait(false); break; } case nameof(MessageEntity.StreamVersion): { streamVersion = await column.Get<int>(cancellationToken).ConfigureAwait(false); break; } case nameof(MessageEntity.TenantId): { tenantId = column.IsDBNull ? null : await column.Get<Guid>(cancellationToken).ConfigureAwait(false); break; } case nameof(MessageEntity.CreatedAt): { createdAt = await column.Get<DateTimeOffset>(cancellationToken).ConfigureAwait(false); break; } case nameof(MessageEntity.CreatedByUserId): { createdByUserId = column.IsDBNull ? null : await column.Get<Guid>(cancellationToken).ConfigureAwait(false); break; } case nameof(MessageEntity.TraceId): { if (column.IsDBNull) traceId = null; else { traceId = new byte[16]; await column.GetStream().ReadExactlyAsync(traceId, 0, traceId.Length, cancellationToken).ConfigureAwait(false); } break; } case nameof(MessageEntity.SpanId): { if (column.IsDBNull) spanId = null; else { spanId = new byte[8]; await column.GetStream().ReadExactlyAsync(spanId, 0, spanId.Length, cancellationToken).ConfigureAwait(false); } break; } case nameof(MessageEntity.PayloadJson): { payloadJson = await column.GetTextReader().ReadToEndAsync(cancellationToken).ConfigureAwait(false); break; } } }
if ( eventId.HasValue is false || string.IsNullOrEmpty(streamType) || streamId.HasValue is false || streamVersion.HasValue is false || createdAt.HasValue is false || payloadJson is null ) { logger?.LogError( """ Some properties are missing: EventId: {EventId} StreamType: {StreamType} StreamId: {StreamId} StreamVersion: {StreamVersion} CreatedAt: {CreatedAt} """, eventId, streamType, streamId, streamVersion, createdAt ); return null; }
if (jsonOptions.GetTypeFromName(streamType) is not Type payloadType) { logger?.LogError("Missing type of payload in event-id {EventId}: {PayloadType}", eventId, streamType); return null; }
if (JsonSerializer.Deserialize(payloadJson, payloadType, jsonOptions.Options) is not object payload) { logger?.LogError( "Payload can not be deserialized on event-id {EventId}, stream {StreamType}/{StreamId}/{StreamVersion}", eventId, streamType, streamId, streamVersion ); return null; }
var metadata = new DomainEventMetadata { EventId = eventId.Value, StreamType = streamType, StreamId = streamId.Value, StreamVersion = streamVersion.Value, TenantId = tenantId, CreatedAt = createdAt.Value, CreatedByUserId = createdByUserId, TraceId = traceId, SpanId = spanId, };
return (metadata, payload); }}