Skip to content

Commit

Permalink
Merge pull request #38 from MrDave1999/feat/choose-db-provider
Browse files Browse the repository at this point in the history
feat: Choose database provider from a configuration file
  • Loading branch information
MrDave1999 authored Aug 29, 2024
2 parents 426f706 + 2c5d6fd commit a26d4ac
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Host/CTF.Host.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Persistence\Persistence.InMemory\Persistence.InMemory.csproj" />
<ProjectReference Include="..\Persistence\Persistence.MariaDB\Persistence.MariaDB.csproj" />
<ProjectReference Include="..\Persistence\Persistence.SQLite\Persistence.SQLite.csproj" />
</ItemGroup>
Expand Down
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();
}
}
}
4 changes: 3 additions & 1 deletion src/Host/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
global using Persistence.MariaDB;
global using Persistence.InMemory;

0 comments on commit a26d4ac

Please sign in to comment.