using Backend.Data.InMemory;
namespace Backend.Data;
///
/// Single place where a storage backend is chosen. To add a real database, implement
/// and register it under a new provider name here;
/// controllers stay untouched.
///
public static class PersistenceServiceCollectionExtensions
{
public static IServiceCollection AddPersistence(
this IServiceCollection services,
IConfiguration configuration)
{
services.Configure(configuration.GetSection(PersistenceOptions.SectionName));
var provider = configuration.GetSection(PersistenceOptions.SectionName).Get()?.Provider
?? PersistenceProviders.InMemory;
switch (provider)
{
case PersistenceProviders.InMemory:
// Singleton because the store itself holds the data.
services.AddSingleton();
break;
default:
throw new InvalidOperationException(
$"Unknown persistence provider '{provider}'. Supported providers: " +
$"'{PersistenceProviders.InMemory}'. Register additional providers in " +
$"{nameof(PersistenceServiceCollectionExtensions)}.");
}
return services;
}
}