Skip to content

Commit

Permalink
Improved flows modules
Browse files Browse the repository at this point in the history
  • Loading branch information
stidsborg committed Jul 5, 2024
1 parent 3b47367 commit cfe904c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
14 changes: 11 additions & 3 deletions Cleipnir.Flows/AspNet/FlowsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FlowsContainer>();

services.AddHostedService(
Expand All @@ -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<Type> FlowsTypes = [];

internal Func<IServiceProvider, Options>? OptionsFunc;
public IServiceCollection Services { get; }

public FlowsConfigurator(IServiceCollection services)
Expand All @@ -50,8 +55,11 @@ public FlowsConfigurator UseStore(IFunctionStore store)
}

public FlowsConfigurator WithOptions(Options options)
=> WithOptions(_ => options);

public FlowsConfigurator WithOptions(Func<IServiceProvider, Options> optionsFunc)
{
Options = options;
OptionsFunc = optionsFunc;
return this;
}

Expand Down
22 changes: 17 additions & 5 deletions Stores/Cleipnir.Flows.MySQL/FlowsModule.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<IServiceProvider, string> connectionStringFunc,
bool initializeDatabase = true,
string tablePrefix = "flows"
)
{
configurator.Services.AddSingleton<IFunctionStore>(
_ =>
sp =>
{
var store = new MySqlFunctionStore(connectionString, tablePrefix: "flows");
var connectionString = connectionStringFunc(sp);
var store = new MySqlFunctionStore(connectionString, tablePrefix);
if (initializeDatabase)
store.Initialize().GetAwaiter().GetResult();

Expand All @@ -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);
}
20 changes: 16 additions & 4 deletions Stores/Cleipnir.Flows.PostgresSql/FlowsModule.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<IServiceProvider, string> connectionStringFunc,
bool initializeDatabase = true,
string tablePrefix = "flows"
)
{
configurator.Services.AddSingleton<IFunctionStore>(
_ =>
sp =>
{
var store = new PostgreSqlFunctionStore(connectionString, tablePrefix: "flows");
var connectionString = connectionStringFunc(sp);
var store = new PostgreSqlFunctionStore(connectionString, tablePrefix);
if (initializeDatabase)
store.Initialize().GetAwaiter().GetResult();

Expand All @@ -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);
}
22 changes: 17 additions & 5 deletions Stores/Cleipnir.Flows.SqlServer/FlowsModule.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<IServiceProvider, string> connectionStringFunc,
bool initializeDatabase = true,
string tablePrefix = "flows"
)
{
configurator.Services.AddSingleton<IFunctionStore>(
_ =>
sp =>
{
var store = new SqlServerFunctionStore(connectionString, tablePrefix: "flows");
var connectionString = connectionStringFunc(sp);
var store = new SqlServerFunctionStore(connectionString, tablePrefix);
if (initializeDatabase)
store.Initialize().GetAwaiter().GetResult();

Expand All @@ -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);
}

0 comments on commit cfe904c

Please sign in to comment.