From b8bb7765fd0fd2a170dfea4c57cff906441f598b Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Thu, 29 Aug 2024 18:13:40 -0500 Subject: [PATCH 1/3] chore: Add project reference --- src/Host/CTF.Host.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Host/CTF.Host.csproj b/src/Host/CTF.Host.csproj index 52a10d1b..7a72641e 100644 --- a/src/Host/CTF.Host.csproj +++ b/src/Host/CTF.Host.csproj @@ -5,6 +5,7 @@ + From f83530bf8e073235978ac255ccde0bce1b0c4514 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Thu, 29 Aug 2024 18:13:52 -0500 Subject: [PATCH 2/3] chore: Update global usings --- src/Host/Usings.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Host/Usings.cs b/src/Host/Usings.cs index 1cee42b0..69ac1bbb 100644 --- a/src/Host/Usings.cs +++ b/src/Host/Usings.cs @@ -5,5 +5,7 @@ global using Microsoft.Extensions.DependencyInjection; global using Microsoft.Extensions.Configuration; global using CTF.Application; +global using CTF.Host.Extensions; global using Persistence.SQLite; -global using Persistence.MariaDB; \ No newline at end of file +global using Persistence.MariaDB; +global using Persistence.InMemory; \ No newline at end of file From 2c5d6fd151f09e1d3efd5d02f9d73440c007b6f7 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Thu, 29 Aug 2024 18:16:47 -0500 Subject: [PATCH 3/3] feat: Choose database provider from a configuration file --- .../Extensions/DatabaseProviderExtensions.cs | 25 +++++++++++++++++++ src/Host/Startup.cs | 4 +-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 src/Host/Extensions/DatabaseProviderExtensions.cs 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 +}