Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Choose database provider from a configuration file #38

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading