From cfe904cc30ef7a86c1f181649205a79f5a48bea6 Mon Sep 17 00:00:00 2001 From: stidsborg Date: Fri, 5 Jul 2024 10:27:15 +0200 Subject: [PATCH] Improved flows modules --- Cleipnir.Flows/AspNet/FlowsModule.cs | 14 +++++++++--- Stores/Cleipnir.Flows.MySQL/FlowsModule.cs | 22 ++++++++++++++----- .../Cleipnir.Flows.PostgresSql/FlowsModule.cs | 20 +++++++++++++---- .../Cleipnir.Flows.SqlServer/FlowsModule.cs | 22 ++++++++++++++----- 4 files changed, 61 insertions(+), 17 deletions(-) diff --git a/Cleipnir.Flows/AspNet/FlowsModule.cs b/Cleipnir.Flows/AspNet/FlowsModule.cs index 295274b..3c47681 100644 --- a/Cleipnir.Flows/AspNet/FlowsModule.cs +++ b/Cleipnir.Flows/AspNet/FlowsModule.cs @@ -14,8 +14,12 @@ public static IServiceCollection AddFlows(this IServiceCollection services, Func { var configurator = new FlowsConfigurator(services); configure(configurator); + + if (configurator.OptionsFunc is null) + services.AddSingleton(new Options()); + else + services.AddSingleton(configurator.OptionsFunc); - services.AddSingleton(configurator.Options); services.AddSingleton(); services.AddHostedService( @@ -27,9 +31,10 @@ public static IServiceCollection AddFlows(this IServiceCollection services, Func public class FlowsConfigurator { - internal Options Options = new(); internal bool EnableGracefulShutdown = false; internal IEnumerable FlowsTypes = []; + + internal Func? OptionsFunc; public IServiceCollection Services { get; } public FlowsConfigurator(IServiceCollection services) @@ -50,8 +55,11 @@ public FlowsConfigurator UseStore(IFunctionStore store) } public FlowsConfigurator WithOptions(Options options) + => WithOptions(_ => options); + + public FlowsConfigurator WithOptions(Func optionsFunc) { - Options = options; + OptionsFunc = optionsFunc; return this; } diff --git a/Stores/Cleipnir.Flows.MySQL/FlowsModule.cs b/Stores/Cleipnir.Flows.MySQL/FlowsModule.cs index c2c9a64..f8c7245 100644 --- a/Stores/Cleipnir.Flows.MySQL/FlowsModule.cs +++ b/Stores/Cleipnir.Flows.MySQL/FlowsModule.cs @@ -1,4 +1,5 @@ -using Cleipnir.Flows.AspNet; +using System; +using Cleipnir.Flows.AspNet; using Cleipnir.ResilientFunctions.MySQL; using Cleipnir.ResilientFunctions.Storage; using Microsoft.Extensions.DependencyInjection; @@ -7,14 +8,18 @@ namespace Cleipnir.Flows.MySQL; public static class FlowsModule { - public static FlowsConfigurator UsePostgresSqlStore( - this FlowsConfigurator configurator, string connectionString, bool initializeDatabase = true + public static FlowsConfigurator UseMySqlStore( + this FlowsConfigurator configurator, + Func connectionStringFunc, + bool initializeDatabase = true, + string tablePrefix = "flows" ) { configurator.Services.AddSingleton( - _ => + sp => { - var store = new MySqlFunctionStore(connectionString, tablePrefix: "flows"); + var connectionString = connectionStringFunc(sp); + var store = new MySqlFunctionStore(connectionString, tablePrefix); if (initializeDatabase) store.Initialize().GetAwaiter().GetResult(); @@ -23,4 +28,11 @@ public static FlowsConfigurator UsePostgresSqlStore( return configurator; } + + public static FlowsConfigurator UseMySqlStore( + this FlowsConfigurator configurator, + string connectionString, + bool initializeDatabase = true, + string tablePrefix = "flows" + ) => UseMySqlStore(configurator, _ => connectionString, initializeDatabase, tablePrefix); } \ No newline at end of file diff --git a/Stores/Cleipnir.Flows.PostgresSql/FlowsModule.cs b/Stores/Cleipnir.Flows.PostgresSql/FlowsModule.cs index c9cafe0..e592c3c 100644 --- a/Stores/Cleipnir.Flows.PostgresSql/FlowsModule.cs +++ b/Stores/Cleipnir.Flows.PostgresSql/FlowsModule.cs @@ -1,4 +1,5 @@ -using Cleipnir.Flows.AspNet; +using System; +using Cleipnir.Flows.AspNet; using Cleipnir.ResilientFunctions.PostgreSQL; using Cleipnir.ResilientFunctions.Storage; using Microsoft.Extensions.DependencyInjection; @@ -8,13 +9,17 @@ namespace Cleipnir.Flows.PostgresSql; public static class FlowsModule { public static FlowsConfigurator UsePostgresSqlStore( - this FlowsConfigurator configurator, string connectionString, bool initializeDatabase = true + this FlowsConfigurator configurator, + Func connectionStringFunc, + bool initializeDatabase = true, + string tablePrefix = "flows" ) { configurator.Services.AddSingleton( - _ => + sp => { - var store = new PostgreSqlFunctionStore(connectionString, tablePrefix: "flows"); + var connectionString = connectionStringFunc(sp); + var store = new PostgreSqlFunctionStore(connectionString, tablePrefix); if (initializeDatabase) store.Initialize().GetAwaiter().GetResult(); @@ -23,4 +28,11 @@ public static FlowsConfigurator UsePostgresSqlStore( return configurator; } + + public static FlowsConfigurator UsePostgresSqlStore( + this FlowsConfigurator configurator, + string connectionString, + bool initializeDatabase = true, + string tablePrefix = "flows" + ) => UsePostgresSqlStore(configurator, _ => connectionString, initializeDatabase, tablePrefix); } \ No newline at end of file diff --git a/Stores/Cleipnir.Flows.SqlServer/FlowsModule.cs b/Stores/Cleipnir.Flows.SqlServer/FlowsModule.cs index 5e9db9d..d1e55c3 100644 --- a/Stores/Cleipnir.Flows.SqlServer/FlowsModule.cs +++ b/Stores/Cleipnir.Flows.SqlServer/FlowsModule.cs @@ -1,4 +1,5 @@ -using Cleipnir.Flows.AspNet; +using System; +using Cleipnir.Flows.AspNet; using Cleipnir.ResilientFunctions.SqlServer; using Cleipnir.ResilientFunctions.Storage; using Microsoft.Extensions.DependencyInjection; @@ -7,14 +8,18 @@ namespace Cleipnir.Flows.SqlServer; public static class FlowsModule { - public static FlowsConfigurator UsePostgresSqlStore( - this FlowsConfigurator configurator, string connectionString, bool initializeDatabase = true + public static FlowsConfigurator UseSqlServerStore( + this FlowsConfigurator configurator, + Func connectionStringFunc, + bool initializeDatabase = true, + string tablePrefix = "flows" ) { configurator.Services.AddSingleton( - _ => + sp => { - var store = new SqlServerFunctionStore(connectionString, tablePrefix: "flows"); + var connectionString = connectionStringFunc(sp); + var store = new SqlServerFunctionStore(connectionString, tablePrefix); if (initializeDatabase) store.Initialize().GetAwaiter().GetResult(); @@ -23,4 +28,11 @@ public static FlowsConfigurator UsePostgresSqlStore( return configurator; } + + public static FlowsConfigurator UseSqlServerStore( + this FlowsConfigurator configurator, + string connectionString, + bool initializeDatabase = true, + string tablePrefix = "flows" + ) => UseSqlServerStore(configurator, _ => connectionString, initializeDatabase, tablePrefix); } \ No newline at end of file