Skip to content

Commit

Permalink
feat: Choose database provider from a configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDave1999 committed Aug 29, 2024
1 parent f83530b commit 2c5d6fd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/Host/Extensions/DatabaseProviderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace CTF.Host.Extensions;

public static class DatabaseProviderExtensions
{
public static void ChooseDatabaseProvider(
this IServiceCollection services,
IConfiguration configuration)
{
var providers = new Dictionary<string, Action>(StringComparer.OrdinalIgnoreCase)
{
{ "InMemory", () => services.AddPersistenceInMemoryServices(configuration) },
{ "SQLite", () => services.AddPersistenceSQLiteServices(configuration) },
{ "MariaDb", () => services.AddPersistenceMariaDBServices(configuration) },
};

string selectedProvider = configuration["DatabaseProvider"];
if (providers.TryGetValue(selectedProvider, out Action databaseProvider))
{
databaseProvider();
return;
}

throw new NotSupportedException($"Provider '{selectedProvider}' is not supported");
}
}
4 changes: 2 additions & 2 deletions src/Host/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public void Configure(IServiceCollection services)
.AddEnvironmentVariables()
.Build();

services.AddPersistenceSQLiteServices(configuration);
services.ChooseDatabaseProvider(configuration);
services.AddApplicationServices();

// Add systems to the services collection
Expand All @@ -25,4 +25,4 @@ public void Configure(IEcsBuilder builder)
.EnablePlayerCommands()
.EnableRconCommands();
}
}
}

0 comments on commit 2c5d6fd

Please sign in to comment.