diff --git a/src/Host/Extensions/DatabaseProviderExtensions.cs b/src/Host/Extensions/DatabaseProviderExtensions.cs new file mode 100644 index 00000000..7b3b815b --- /dev/null +++ b/src/Host/Extensions/DatabaseProviderExtensions.cs @@ -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(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"); + } +} diff --git a/src/Host/Startup.cs b/src/Host/Startup.cs index 21f954f4..628f82e0 100644 --- a/src/Host/Startup.cs +++ b/src/Host/Startup.cs @@ -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 @@ -25,4 +25,4 @@ public void Configure(IEcsBuilder builder) .EnablePlayerCommands() .EnableRconCommands(); } -} \ No newline at end of file +}