Beispiel #1
0
        private static void ConfigurePgSqlStreamStore(Config config)
        {
            var connectionString = "Host=localhost;Port=5432;User Id=postgres;Database=postgres";
            var settings         = new PostgresStreamStoreSettings(connectionString);
            var pgStreamStore    = new PostgresStreamStore(settings);

            config.Container.Register <IStreamStore>(pgStreamStore);
            pgStreamStore.CreateSchemaIfNotExists().GetAwaiter().GetResult();
        }
        private static async Task <IStreamStore> CreatePostgresStreamStore(
            string connectionString,
            string schema,
            CancellationToken cancellationToken)
        {
            var connectionStringBuilder = new NpgsqlConnectionStringBuilder(connectionString);

            using (var connection = new NpgsqlConnection(new NpgsqlConnectionStringBuilder(connectionString)
            {
                Database = null
            }.ConnectionString))
            {
                bool exists;
                await connection.OpenAsync(cancellationToken).ConfigureAwait(false);

                using (var command = new NpgsqlCommand(
                           $"SELECT 1 FROM pg_database WHERE datname = '{connectionStringBuilder.Database}'",
                           connection))
                {
                    exists = await command.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false)
                             != null;
                }

                if (!exists)
                {
                    using (var command = new NpgsqlCommand(
                               $"CREATE DATABASE {connectionStringBuilder.Database}",
                               connection))
                    {
                        await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
                    }
                }

                var settings = new PostgresStreamStoreSettings(connectionString);

                if (schema != null)
                {
                    settings.Schema = schema;
                }

                var streamStore = new PostgresStreamStore(settings);

                await streamStore.CreateSchemaIfNotExists(cancellationToken);

                return(streamStore);
            }
        }
        public static IServiceCollection AddPostgresEventStore(this IServiceCollection services,
                                                               Action <PostgresEventSourcingOptions>?optionsAccessor = null, params Assembly[] eventAssemblies)
        {
            var postgresOptions = new PostgresEventSourcingOptions();

            optionsAccessor?.Invoke(postgresOptions);
            // ReSharper disable once RedundantAssignment
            services.AddEventStore(eventSourcingOptions => eventSourcingOptions = postgresOptions, eventAssemblies)
            .Configure(optionsAccessor)
            .AddSingleton <PostgresCheckpointManager>()
            .TryAddSingleton <PostgresConnection>();

            var serviceProvider = services.BuildServiceProvider();

            if (!(serviceProvider.GetService <IStreamStore>() is PostgresStreamStore))
            {
                services.AddSingleton <IStreamStore>(sp =>
                {
                    var connection  = sp.GetRequiredService <PostgresConnection>().Build();
                    var streamStore = new PostgresStreamStore(
                        new PostgresStreamStoreSettings(connection.ConnectionString)
                    {
                        Schema = postgresOptions.Schema
                    });

                    if (!postgresOptions.CreateSchemaIfNotExists)
                    {
                        return(streamStore);
                    }

                    streamStore.CreateSchemaIfNotExists().Wait();
                    sp.GetRequiredService <PostgresCheckpointManager>().CreateSchemaIfNotExists().Wait();

                    return(streamStore);
                });
            }

            services.TryAddSingleton <GetGlobalCheckpoint>(sp =>
                                                           sp.GetRequiredService <PostgresCheckpointManager>().GetGlobalCheckpoint);

            services.TryAddSingleton <SetGlobalCheckpoint>(sp =>
                                                           sp.GetRequiredService <PostgresCheckpointManager>().SetGlobalCheckpoint);

            return(services);
        }